-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
82 lines (81 loc) · 2.63 KB
/
Graph.java
File metadata and controls
82 lines (81 loc) · 2.63 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
//import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Graph<T>{
private HashMap<T,ArrayList<T>> nodes;
private HashMap<T,HashMap<T,Integer>> edgeWeight;
Graph(){
nodes=new HashMap<T,ArrayList<T>>();
edgeWeight=new HashMap<T,HashMap<T,Integer>>();
}
public void addVertex(T V){
nodes.put(V,new ArrayList<T>());
edgeWeight.put(V,new HashMap<T,Integer>());
}
public void addEdge(T u,T v){
nodes.get(u).add(v);
nodes.get(v).add(u);
setEdgeWeight(u, v, 0);
}
public void setEdgeWeight(T u,T v,int weight){
edgeWeight.get(u).put(v,weight);
edgeWeight.get(v).put(u,weight);
}
public int getEdgeWeight(T u,T v){
return edgeWeight.get(u).get(v);
}
public ArrayList<T> getAdjacentNodes(T vertex){
return nodes.get(vertex);
}
public void printGraph(){
Set<T> vertices=nodes.keySet();
Iterator<T> i=vertices.iterator();
while(i.hasNext()){
T a=i.next();
System.out.print(a+": ");
for(int j=0;j<nodes.get(a).size();j++){
System.out.print(nodes.get(a).get(j)+" ");
}
System.out.println();
}
}
public ArrayList<T> getVertices(){
Set<T> s=nodes.keySet();
Iterator<T> i=s.iterator();
ArrayList<T> vertices=new ArrayList<T>();
while(i.hasNext()){
vertices.add(i.next());
}
return vertices;
}
public ArrayList<ArrayList<T>> getEdges(){
ArrayList<T> vertices=getVertices();
HashMap<T,Integer> map=new HashMap<T,Integer>();
for(int i=0;i<vertices.size();i++){
map.put(vertices.get(i), 0);
}
ArrayList<ArrayList<T>> edges=new ArrayList<ArrayList<T>>();
for(int i=0;i<vertices.size();i++){
ArrayList<T> adj=getAdjacentNodes(vertices.get(i));
for(int j=0;j<adj.size();j++){
if(map.containsKey(adj.get(j))){
ArrayList<T> temp=new ArrayList<T>();
temp.add(vertices.get(i));
temp.add(adj.get(j));
edges.add(temp);
}
}
map.remove(vertices.get(i));
}
return edges;
}
/*public static void main(String[] args) {
Graph<String> g=new Graph<String>();
g.addVertex("A");
g.addVertex("B");
g.addEdge("A", "B");
g.printGraph();
}*/
}