-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGraph.java
148 lines (130 loc) · 3.52 KB
/
Graph.java
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
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Graph<T>
{
private List<Edge<T>> allEdges;
private Map<Long,Vertex<T>> allVertex;
boolean isDirected = false;
public Graph()
{
allEdges = new ArrayList<Edge<T>>();
allVertex = new HashMap<Long,Vertex<T>>();
}
public Graph(boolean isDirected)
{
allEdges = new ArrayList<Edge<T>>();
allVertex = new HashMap<Long,Vertex<T>>();
this.isDirected = isDirected;
}
public void addEdge(long id1, long id2)
{
addEdge(id1,id2,0);
}
//This works only for directed graph because for undirected graph we can end up
//adding edges two times to allEdges
public void addVertex(Vertex<T> vertex)
{
/*
* It adds a vertex to the graph.
* it searches in the graph if it already has, if it has then it does nothing.
* else it puts the object of vertex class in the hashmap mapped with its id and puts all its edges into the edges list
*
*/
if(allVertex.containsKey(vertex.getId()))
{
return;
}
allVertex.put(vertex.getId(), vertex);
for(Edge<T> edge : vertex.getEdges())
{
allEdges.add(edge);
}
}
public Vertex<T> addSingleVertex(long id)
{
if(allVertex.containsKey(id))
{
return allVertex.get(id);
}
Vertex<T> v = new Vertex<T>(id);
allVertex.put(id, v);
return v;
}
public Vertex<T> getVertex(long id)
{
return allVertex.get(id);
}
public void addEdge(long id1,long id2, int weight)
{
Vertex<T> vertex1 = null;
if(allVertex.containsKey(id1))
{
vertex1 = allVertex.get(id1);
}else
{
vertex1 = new Vertex<T>(id1);
allVertex.put(id1, vertex1);
}
Vertex<T> vertex2 = null;
if(allVertex.containsKey(id2))
{
vertex2 = allVertex.get(id2);
}
else
{
vertex2 = new Vertex<T>(id2);
allVertex.put(id2, vertex2);
}
Edge<T> edge = new Edge<T>(vertex1,vertex2,isDirected,weight);
allEdges.add(edge);
vertex1.addAdjacentVertex(edge, vertex2);
if(!isDirected)
{
vertex2.addAdjacentVertex(edge, vertex1);
}
}
public boolean isAdjacent(int id1,int id2)
{
Vertex<T> v1=null;
if(allVertex.containsKey(id1))
v1=allVertex.get(id1);
else
return false;
Vertex<T> v2=null;
if(allVertex.containsKey(id2))
v2=allVertex.get(id2);
else
return false;
return v1.isAdjacent(v2);
}
public List<Edge<T>> getAllEdges()
{
return allEdges;
}
public Collection<Vertex<T>> getAllVertex()
{
return allVertex.values();
}
public void setDataForVertex(long id, T data)
{
if(allVertex.containsKey(id))
{
Vertex<T> vertex = allVertex.get(id);
vertex.setData(data);
}
}
@Override
public String toString()
{
StringBuffer buffer = new StringBuffer();
for(Edge<T> edge : getAllEdges())
{
buffer.append(edge.getVertex1() + " " + edge.getVertex2() + " " + edge.getWeight());
buffer.append("\n");
}
return buffer.toString();
}
}