-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.pde
More file actions
181 lines (176 loc) · 5.02 KB
/
Graph.pde
File metadata and controls
181 lines (176 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.util.Set;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeSet;
import java.util.Collections;
class Graph{
public Graph(){
vertexAdjacencyMap = new HashMap<Vertex,TreeSet<Edge>> ();
drawVertices = drawEdges = true;
directed = false;
weighted = true;
}
public Graph(Graph other){
vertexAdjacencyMap = other.vertexAdjacencyMap;
drawVertices = other.drawVertices;
drawEdges = other.drawEdges;
weighted = other.weighted;
directed = other.directed;
setLabelInterface(other.labelInterface);
}
public void addVertex(Vertex a){
if(a != null && !vertexAdjacencyMap.containsKey(a)){
if(labelInterface!= null)a.setInterface(labelInterface);
vertexAdjacencyMap.put(a, new TreeSet<Edge>());
}
}
public void addGraph(Graph graph){
vertexAdjacencyMap.putAll(graph.vertexAdjacencyMap);
}
public void addEdge(Vertex source,Vertex dest, int weight){
Edge e = getEdge(source, dest);
Edge eComp = getEdge(dest, source);
addVertex(source);
addVertex(dest);//tries to add if not in
if(!directed){
if((e != null && eComp != null)){
//update
e.setWeight(weight);
eComp.setWeight(weight);
}else{
e = new Edge(source,dest,weight);
eComp = new Edge(dest, source,weight);
e.setInterface(labelInterface);
eComp.setInterface(labelInterface);
vertexAdjacencyMap.get(source).add(e);
vertexAdjacencyMap.get(dest).add(eComp);
}
}
}
public void setDrawVertices(boolean drawVertices){
this.drawVertices = drawVertices;
}
public void setDrawEdges(boolean drawEdges){
this.drawEdges = drawEdges;
}
public void setWeighted(boolean isWeighted){
this.weighted = isWeighted;
}
public void setDirected(boolean isDirected){
this.directed = isDirected;
}
public void setHighlightEdges(boolean isHighlighted){
for(Edge e:getEdgeSet())
e.setHighlight(isHighlighted);
}
public void setHighlightVertices(boolean isHighlighted){
for(Vertex v: getVertexSet())
v.setHighlight(isHighlighted);
}
public Vertex getVertex(String id){
for(Vertex v: getVertexSet()){
if(String.valueOf(v.getID()) == id)
return v;
}
return null;
}
public void drawEdges(){
if(drawEdges)
for(Edge e : getEdgeSet())
e.draw();
}
public boolean isDrawVertices()
{
return drawVertices;
}
public boolean isDrawEdges()
{
return drawEdges;
}
public void drawVertices(){
if(drawVertices)
for(Vertex v: getVertexSet())
v.draw();
}
public ArrayList<Vertex> getVertexSet(){
ArrayList<Vertex> sortList= new ArrayList(vertexAdjacencyMap.keySet());
Collections.sort(sortList);
return sortList;
}
public int getVertexCount(){
return (new ArrayList(vertexAdjacencyMap.keySet())).size();
}
public Edge getEdge(Vertex source, Vertex dest){
if(source == null || dest == null) return null;
TreeSet<Edge> edgeSet = vertexAdjacencyMap.get(source);
if(edgeSet == null) return null;
boolean found = false;
Edge e = null;
Iterator it = edgeSet.iterator();
while(it.hasNext() && !found){
e= (Edge)it.next();
if(e.getSource() == source && e.getDest() == dest){
found =true;
}
}
if(found)
return e;
else
return null;
}
public void updateEdgeWeight(Vertex source, Vertex dest, int weight){
TreeSet<Edge>sourceEdgeSet = vertexAdjacencyMap.get(source);
TreeSet<Edge>destEdgeSet = vertexAdjacencyMap.get(dest);
boolean found = false;
Edge e = null;
Iterator it = sourceEdgeSet.iterator();
while(it.hasNext() && !found){
e= (Edge)it.next();
if(e.getDest() == dest){
e.setWeight(weight);
found = true;
}
}
found = false;
//update destinations edges
it = destEdgeSet.iterator();
while(it.hasNext() && !found){
e= (Edge)it.next();
if(e.getDest() == source){
e.setWeight(weight);
found = true;
}
}
}
public ArrayList<Edge> getAdjacentEdges(Vertex v){
ArrayList<Edge> sortList = new ArrayList<Edge>(vertexAdjacencyMap.get(v));
Collections.sort(sortList);
return sortList;
}
public ArrayList<Edge> getEdgeSet(){
ArrayList<Edge> sortList = new ArrayList<Edge>();
for(Vertex v : getVertexSet())
sortList.addAll(vertexAdjacencyMap.get(v));
Collections.sort(sortList);
return sortList;
}
public void clear(){
vertexAdjacencyMap.clear();
}
public void setLabelInterface( LabelInterface labelInterface){
if(labelInterface != null){
this.labelInterface = labelInterface;
for(Edge e: getEdgeSet())
e.setInterface(labelInterface);
for(Vertex v: getVertexSet())
v.setInterface(labelInterface);
}
}
public LabelInterface getInterface(){
return labelInterface;
}
LabelInterface labelInterface;
HashMap<Vertex,TreeSet<Edge>> vertexAdjacencyMap;
boolean drawVertices, drawEdges;
boolean directed, weighted;
}