1 graph theory © dave bockus. 2 adjacency matrix 1 2 3 67 5 4
TRANSCRIPT
![Page 1: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/1.jpg)
1
Graph Theory
© Dave Bockus
![Page 2: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/2.jpg)
2
Adjacency Matrix
1 2
3
6 7
54
![Page 3: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/3.jpg)
3
Adjacency List1 2
3
6 7
54
1
2
3
4
5
6
7
2
4
3
4
5
7
6
7
4 3
6
6
![Page 4: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/4.jpg)
4
Example DAG
I E
A
C
B
H
F
D
JG
I B D G C E F J A H or C E G A I B F J D H
Possible Topological Order
![Page 5: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/5.jpg)
5
TopSortvoid topsort( ) throws CycleFound {
Queue q;int counter = 0;Vertex v,w;
q = new Queue();for each vertex v if (indegree == 0) q.enqueue(v);
while (!q.empty()) { v = q.dequeue(); v.topNum = ++counter; for each w adjacent to v if (--w.indegree==0) q.enqueue(w);}
if (counter != NUM_VERTICES) throw new CycleFound();}
Vertices will remain un-enumerated if a cycle exists
Initialize Q with vertices of indegree 0
Decrement indegrees of vertices w, if 0 then enqueue
![Page 6: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/6.jpg)
6
TopSort Example
Queue
I E
A
C
B
H
F
D
JG
Output
Load Q with initial v with indegree == 0
BGCI
I
E
C G B
D
E
A F
D A F
J
J
H
H
![Page 7: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/7.jpg)
7
Shortest Path Unweighted edges
Q.Enqueue(v0);
while (!Q.IsEmpty) {V = Q.Dequeue();for (Each W adjacent to V)
if(T[W].Dist == Maxint) { T[W].Dist = T[V].Dist +
1; T[W].Path = V; Q.Enqueue(W); }}
Breadth First Search Algorithm
![Page 8: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/8.jpg)
8
Shortest Path - Unweighted edges
1 2
3
6 7
54
Queue
Enqueue Vo
3 6
1 v3
1
1 v3
4
2 v1
2 v1
2
No vertices adjacent to 6
5
v23
Ignore V4
V4 != MaxInt
7
3 v4Ignore V6
V6 != MaxInt
Ignore V4
V4 != MaxIntIgnore V7
V7 != MaxInt
Ignore V6
V6 != MaxInt
Queue is now empty so stop
![Page 9: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/9.jpg)
9
Dijkstra’s Algorithm
Q.Enqueue(v0);while (!Q.IsEmpty) { do { V = Q.Dequeue(); while (T[V].Known);
T[V].Known = true; for (Each W adjacent to V) if(T[W].Dist > T[V].Dist + C(V,W) { T[W].Dist = T[V].Dist + C(V,W); T[W].Path = V; Q.Enqueue(W); }}
Only accept unknown edges
Modify the path if an improvement to dv exists
![Page 10: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/10.jpg)
10
Dijkstra’s Algorithm cont...
1 2
3
6 7
54
4
4
1
1
2 2
2
3
65
10
8
PQueue
Enqueue Vo
1
0
4
1
1 v1
2
2
2 v1
1
1
3
3
3 v4
7
5
v45
6
9
v49
1
No improvement to v4 so skip
5
12
v212
1
No improvement to v1 so skip
8 v3
6
8
Update dv and pv to reflect
improvement
1
6
6
Update dv and pv to reflect
improvement
v761
V6 is already known so ignore
1
No improvement to v4 so skip
No improvement to v7 so skipQueue is now empty so stop
![Page 11: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/11.jpg)
11
Kruskal's Algorithm
Build priority Queue of all edges
Edges_Accepted = 0;
while (Edges_Accepted < NumVertex - 1) {
E = Dequeue(); // E=(u,v)
if (!Connected(E.u,E.v)) {
Edges_Accepted ++;
T[Action] = true;
}
}
![Page 12: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/12.jpg)
12
Kruskal's Algorithm cont...
1 2
3
6 7
54
4
4
1
1
2 2
2
3
65
10
8
Edge Weight ActionV1,V4 1 0V6,V7 1 0V1,V2 2 0V3,V4 2 0V4,V5 2 0V2,V4 3 0V1,V3 4 0V4,V7 4 0V3,V6 5 0V5,V7 6 0V4,V6 8 0V2,V5 10 0
1
1
1
1
1
Edge causes a cycle, so ignore
Edge causes a cycle, so ignore
1
V-1 Edges accepted so stop
Total path lengths:
= 1 + 1 + 2 + 2 + 2 + 4
= 12
![Page 13: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/13.jpg)
13
Kruskal's Algorithm cont...
void kruskal() {
int edgesAccepted =0;
DisjSet ds = new DisjSets(NUM-Vertices);
PQ<Edge> pq = new PriorityQueue<Edge>(getEdges());
Edge e;
Vertix u,v;
while (edgesAccepted < NUM_VERTICES -1) {
e = pq.deleteMin(); //remove the smallest edge from pq E=(u,v);
SetType uset = ds.find(u);
SetType vset = ds.find(v);
if (uset != vset){
edgesAccepted++;
ds.union(uset,vset);
}
}
}
![Page 14: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/14.jpg)
14
Kruskal's Algorithm cont...
• Typically the algorithm will maintain a forest of sets– The concept would be to reduce the forest to just 1 set.
– A free tree
• This implies set operations such as:– Union(Uset,Vset)
• Join 2 sets Uset & Vset
– Find(u)• Return the set with vertex u in it.
– Compare(Uset,Vset)• Is Uset equal Vset
• Set operations can become costly.
![Page 15: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/15.jpg)
15
Kruskal's Algorithm cont...
• Determining cycles without using sets– Support an Adjacency list of accepted edges
– Want to add edge (u,v)
– Build a tree from u of currently accepted edges.• If v in the tree, then (u,v) will cause a cycle.
• Else we add (u,v) as an accepted edge.
• Additional Data Structures are required– Adjacency list of accepted edges
– Integer array size n used for book keeping• Only enumerate a vertex once.
• If graph has directed edges then we need to determine:– u contains v
– v contains u
![Page 16: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/16.jpg)
16
Kruskal's Algorithm cont...for (i==1; i <= n; i++) //Initialize Known to 0
Known[i]=0;
boolean Find(u,v){
boolean cycle = false;
Known[u]=1; //Mark u as processed
if (u == v) return true; //We found a cycle
for (each w adjacent to u)
if (Known[w]==0) //Only search vertices w
cycle = cycle || Find(w,v); //which are unprocessed
return cycle;
}
If we ever find a cycle (u==v) then we return true. This value is passed back true the recursion.
![Page 17: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/17.jpg)
17
Prim’s Algorithm
• Basic Idea1. Build a tree starting at Vo=u.
2. Select vertex v which is closest to u and add (u,v) to tree.
3. Find next closes vertex v to tree and add.
4. Repeat 3 Until all v have been consumed.
![Page 18: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/18.jpg)
18
Prim’s Algorithm
Q.Enqueue(V0,V0);Vertices=1while (Vertices++ < |V|) { do { E = Q.Dequeue(); while (T[v].Known);
T[v].Known = true; for (Each w adjacent to v) if(T[w].Dist > C(v,w) && !T[w].known){ T[w].Dist = C(v,w); T[w].Path = v; Q.Enqueue(v,w); }}
Very Similar to Dijkstra’s Algorithm. dv now only holds the edge weight.
PQ of edges c(u,v), where u is in the tree and v is not.
Where E = (u,v)
![Page 19: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/19.jpg)
19
Prim’s Algorithm Cont...
PQ
1 2
3
6 7
54
4
4
1
2 7
2
3
65
10
8
1
C(1,1)
1
1
C(1,4) C(1,2) C(1,3)
1
4
1 V1
C(4,1) C(1,2) C(4,3) C(4,2) C(4,6)C(1,3) C(4,7) C(4,5)
2
1 2 V1
C(2,4)C(2,1)C(4,3) C(4,2) C(4,6)C(1,3) C(4,7) C(4,5) C(2,5)
3
1 2 V4
C(2,4)C(3,4)C(2,1) C(4,2) C(4,6)C(1,3) C(4,7) C(4,5) C(2,5)C(3,1) C(3,6)
4 V4
7
1
C(4,6)C(4,5) C(2,5)C(3,1) C(3,6) C(7,5)C(7,4)C(7,6)
6
1 1 V7
C(4,6)C(4,5) C(2,5)C(3,1) C(3,6) C(7,5)C(7,4)C(6,7) C(6,3) C(6,4)
5
1 6 V7
![Page 20: 1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4](https://reader036.vdocuments.site/reader036/viewer/2022082610/56649cea5503460f949b5d27/html5/thumbnails/20.jpg)
20
Prim’s Algorithm Cont..
• The Algorithm stops when we have accepted |V| vertices.
• We can read the accepted edges from the table directly.