-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph2.java
More file actions
167 lines (131 loc) · 3.85 KB
/
Copy pathGraph2.java
File metadata and controls
167 lines (131 loc) · 3.85 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
import java.util.List;
import java.util.Iterator;
import java.util.Set;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Queue;
import java.util.PriorityQueue;
import java.util.BitSet;
enum GraphType{
DIR, UNDIR
}
abstract class Graph2<T>{
int V; //NumVertices
GraphType type;
public Graph2(GraphType type){
V = 0;
this.type = type;
}
public int get_num_vertices(){
return V;
}
public Vertex<T> add_vertex(int id, T data){
Vertex<T> vo = implement_add_vertex(id, data);
V=V+1;
return vo;
}
public abstract Vertex<T> implement_add_vertex(int id, T data);
public void add_edge(int uid, T udata, int vid, T vdata, int w){
implement_add_edge(uid, udata, vid, vdata, w);
}
public abstract void implement_add_edge(int uid, T udata, int vid, T vdata, int w);
public List<Edge<T>> get_neighbors(Vertex<T> v){
List<Edge<T>> ll = implement_get_neighbors(v);
return ll;
}
public abstract List<Edge<T>> implement_get_neighbors(Vertex<T> v);
public Set<Vertex<T>> get_vertices(){
Set<Vertex<T>> s = implement_get_vertices();
return s;
}
public abstract Set<Vertex<T>> implement_get_vertices();
//DFS (Recursive version)
public void dfs_rec(Vertex<T> v){
//Visit v
if(v.get_visited() == Status.Unvisited){
v.set_visited(Status.Visited);
visit(v);
//Get all one hop neighbors of v
List<Edge<T>> ll = get_neighbors(v);
Iterator<Edge<T>> it = ll.iterator();
while(it.hasNext()){
Edge<T> w = it.next();
dfs_rec(w.get_to());
}
}
}
public void visit(Vertex<T> v){
System.out.println("Visiting the vertex:" + v.get_data());
}
public void min_spanning_tree(Vertex<T> root){
//Set<Vertex> mintree = new HashSet<Vertex>();
//A priority queue to hold all vertices by their key value
//(in non decreasing order)
Queue<Vertex<T>> q = new PriorityQueue<Vertex<T>>();
//A BitSet to avoid using queue contains method
BitSet b = new BitSet(V);
//Set root vertex's key to 0,
//Rest all have key = Integer.MAX_VALUE
root.set_key(0);
//Get all vertices in this graph
Set<Vertex<T>> vertices = get_vertices();
//Add all vertices to a priority queue
for(Vertex<T> v: vertices){
q.offer(v);
}
while(!q.isEmpty()){
Vertex<T> u = q.poll();
b.set(u.get_id());
//Add u to spanning tree.
//May be not needed
//parent pointers will help.
//Update key for all adjacent vertices to u
List<Edge<T>> elist = u.get_edge_list();
Iterator<Edge<T>> it = elist.iterator();
while(it.hasNext()){
Edge<T> e = it.next();
Vertex<T> w = e.get_to();
//if(q.contains(w) && e.get_weight() < w.get_key()){
if(!b.get(w.get_id()) && e.get_weight() < w.get_key()){
w.set_key(e.get_weight());
w.set_parent(u);
}
}
}
//return mintree;
}
//Complexity
//O(V*Extractmin + E*DecreaseKey)
//=O(VlogV + ElogV),
//Using Fibonnaci heap: O(VlogV + E)
public void dijkstra_shortest_path(Vertex<T> s){
//A priority queue to hold all vertices in decreasing order of key
//key is the distance estimate from source for each vertex.
//Initially, it is = Integer.MAX_VALUE for each vertex.
Queue<Vertex<T>> q = new PriorityQueue<Vertex<T>>();
//Set source vertex's key to 0;
s.set_key(0);
//Add all vertices to q
Set<Vertex<T>> vertices = get_vertices();
for(Vertex<T> v: vertices){
q.offer(v);
}
while(!q.isEmpty()){
//Add u to shortest path
Vertex<T> u = q.poll();
//Relax all adjacent edges for u.
List<Edge<T>> elist = u.get_edge_list();
Iterator<Edge<T>> it = elist.iterator();
while(it.hasNext()){
Edge<T> e = it.next();
Vertex<T> w = e.get_to();
if(w.get_key() > (u.get_key() + e.get_weight())){
w.set_key(u.get_key() + e.get_weight());
w.set_parent(u);
}
}
}
}
}