Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4.

Similar presentations


Presentation on theme: "1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4."— Presentation transcript:

1 1 Graph Theory © Dave Bockus

2 2 Adjacency Matrix 1 2 3 67 5 4

3 3 Adjacency List 1 2 3 67 5 4 12345671234567 2 4 3 4 5 7 6 7 43 6 6

4 4 Example DAG IE A C B H F D J G I B D G C E F J A H or C E G A I B F J D H Possible Topological Order

5 5 TopSort void 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

6 6 TopSort Example Queue IE A C B H F D J G Output Load Q with initial v with indegree == 0 B G C I I E C G B D E AF D A F J J H H

7 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

8 8 Shortest Path - Unweighted edges 1 2 3 67 5 4 Queue Enqueue Vo 36 1 v3 1 1 4 2 v1 2 2 No vertices adjacent to 6 5 v2 3 Ignore V4 V4 != MaxInt 7 3 v4 Ignore V6 V6 != MaxInt Ignore V4 V4 != MaxInt Ignore V7 V7 != MaxInt Ignore V6 V6 != MaxInt Queue is now empty so stop

9 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

10 10 Dijkstra’s Algorithm cont... 1 2 3 67 5 4 4 4 1 1 2 2 2 3 6 5 10 8 PQueue Enqueue Vo 1 0 4 1 1v1 2 2 2 1 1 3 3 3 v4 7 5 5 6 9 9 1 No improvement to v4 so skip 5 12 v2 12 1 No improvement to v1 so skip 8 v3 6 8 Update dv and pv to reflect improvement 1 6 6 v76 1 V6 is already known so ignore 1 No improvement to v4 so skip No improvement to v7 so skip Queue is now empty so stop

11 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; }

12 12 Kruskal's Algorithm cont... 1 2 3 67 5 4 4 4 1 1 2 2 2 3 6 5 10 8 1 1 1 1 1 Edge causes a cycle, so ignore 1 V-1 Edges accepted so stop Total path lengths: = 1 + 1 + 2 + 2 + 2 + 4 = 12

13 13 Kruskal's Algorithm cont... void kruskal() { int edgesAccepted =0; DisjSet ds = new DisjSets(NUM-Vertices); PQ pq = new PriorityQueue (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); }

14 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.

15 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

16 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.

17 17 Prim’s Algorithm Basic Idea 1.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.

18 18 Prim’s Algorithm Q.Enqueue(V 0,V 0 ); Vertices=1 while (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)

19 19 Prim’s Algorithm Cont... PQ 1 2 3 67 5 4 4 4 1 2 7 2 3 6 5 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) 4V4 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

20 20 Prim’s Algorithm Cont.. The Algorithm stops when we have accepted |V| vertices. We can read the accepted edges from the table directly.


Download ppt "1 Graph Theory © Dave Bockus. 2 Adjacency Matrix 1 2 3 67 5 4."

Similar presentations


Ads by Google