1
2
3
4
5
6
7
8
9
10
11
12 package edu.uci.ics.jung.graph.util;
13
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18
19 import edu.uci.ics.jung.graph.Graph;
20
21
22
23
24
25
26
27 public class IncidentEdgeIndexFunction<V,E> implements EdgeIndexFunction<V,E>
28 {
29 protected Map<E, Integer> edge_index = new HashMap<E, Integer>();
30
31 private IncidentEdgeIndexFunction() {
32 }
33
34
35
36
37
38
39 public static <V,E> IncidentEdgeIndexFunction<V,E> getInstance() {
40 return new IncidentEdgeIndexFunction<V,E>();
41 }
42
43
44
45
46
47
48 public int getIndex(Graph<V,E> graph, E e)
49 {
50 Integer index = edge_index.get(e);
51 if(index == null) {
52 Pair<V> endpoints = graph.getEndpoints(e);
53 V u = endpoints.getFirst();
54 V v = endpoints.getSecond();
55 if(u.equals(v)) {
56 index = getIndex(graph, e, v);
57 } else {
58 index = getIndex(graph, e, u, v);
59 }
60 }
61 return index.intValue();
62 }
63
64 protected int getIndex(Graph<V,E> graph, E e, V u, V v) {
65 Collection<E> commonEdgeSet = new HashSet<E>(graph.getIncidentEdges(u));
66 int count=0;
67 for(E other : commonEdgeSet) {
68 if(e.equals(other) == false) {
69 edge_index.put(other, count);
70 count++;
71 }
72 }
73 edge_index.put(e, count);
74 return count;
75 }
76
77 protected int getIndex(Graph<V,E> graph, E e, V v) {
78 Collection<E> commonEdgeSet = new HashSet<E>();
79 for(E another : graph.getIncidentEdges(v)) {
80 V u = graph.getOpposite(v, another);
81 if(u.equals(v)) {
82 commonEdgeSet.add(another);
83 }
84 }
85 int count=0;
86 for(E other : commonEdgeSet) {
87 if(e.equals(other) == false) {
88 edge_index.put(other, count);
89 count++;
90 }
91 }
92 edge_index.put(e, count);
93 return count;
94 }
95
96
97
98
99
100
101
102
103 public void reset(Graph<V,E> graph, E e) {
104 Pair<V> endpoints = graph.getEndpoints(e);
105 getIndex(graph, e, endpoints.getFirst());
106 getIndex(graph, e, endpoints.getFirst(), endpoints.getSecond());
107 }
108
109
110
111
112
113 public void reset()
114 {
115 edge_index.clear();
116 }
117 }