/
CSCE  4930 Data Structures and Algorithms Prof. Amr Goneid AUC CSCE  4930 Data Structures and Algorithms Prof. Amr Goneid AUC

CSCE 4930 Data Structures and Algorithms Prof. Amr Goneid AUC - PowerPoint Presentation

test
test . @test
Follow
383 views
Uploaded On 2019-11-04

CSCE 4930 Data Structures and Algorithms Prof. Amr Goneid AUC - PPT Presentation

CSCE 4930 Data Structures and Algorithms Prof Amr Goneid AUC Part 10 Graphs Prof Amr Goneid AUC 1 Graphs Prof Amr Goneid AUC 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties ID: 763033

goneid amr auc prof amr goneid prof auc graph edge edges vertices val algorithm vertex int tree graphs connected

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CSCE 4930 Data Structures and Algorithm..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

CSCE 4930Data Structures and Algorithms Prof. Amr GoneidAUCPart 10. Graphs Prof. Amr Goneid, AUC 1

Graphs Prof. Amr Goneid, AUC 2

GraphsBasic DefinitionsPaths and CyclesConnectivityOther Properties RepresentationExamples of Graph Algorithms:Graph Traversal Shortest PathsMinimum Cost Spanning Trees Prof. Amr Goneid, AUC 3

1. Basic DefinitionsA graph G (V,E) can be definedas a pair (V,E) , where V is a setof vertices, and E is a set of edges between the vertices E = {(u,v) | u, v  V}. e.g. V = {A,B,C,D,E,F,G} E = {( A,B),(A,F),(B,C),(C,G),(D,E),(D,G),(E,F),(F,G)} If no weights are associated with the edges, an edge is either present(“1”) or absent (“0”). Prof. Amr Goneid, AUC 4 A F G B E D C

Basic DefinitionsA graph is like a road map. Cities are vertices. Roads from city to city are edges.You could consider junctions to be vertices, too. If you don't want to count them as vertices, a road may connect more than two cities. So strictly speaking you have hyperedges in a hypergraph.If you want to allow more than one road between each pair of cities, you have a multigraph, instead. Prof. Amr Goneid, AUC 5

Basic DefinitionsAdjacency: If vertices u,v havean edge e = (u,v) | u, v  V thenu and v are adjacent. A weighted graph has a weight associated with each edge. Undirected Graph is a graph in which the adjacency is symmetric, i.e., e = ( u,v ) = ( v,u ) A Sub-Graph: has a subset of the vertices and the edges Prof. Amr Goneid, AUC 6 A F G B E D C 2 3 1 5 1 2 2 4

Basic DefinitionsDirected Graph: is a graph in whichadjacency is not symmetric,i.e., (u,v)  ( v,u) Such graphs are also called “Digraphs” Directed Weighted Graph: A directed graph with a weight for each edge. Also called a network. Prof. Amr Goneid, AUC 7 A F G B E D C 2 3 1 5 1 2 2 4

2. Paths & Cycles Path: A list of vertices of a graph where each vertex has an edge from it to the next vertex. Simple Path: A path that repeats no vertex. Cycle: A path that starts and ends at the same vertex and includes other vertices at most once . Prof. Amr Goneid, AUC 8 A F G B E D C A F G B E D C

Directed Acyclic Graph (DAG) Directed Acyclic Graph (DAG): A directed graph with no path that starts and ends at the same vertex Prof. Amr Goneid, AUC 9 A F G B E D C

Hamiltonian Cycle Hamiltonian Cycle: A cycle that includes all other vertices only once, e.g. {D,B,C,G,A,F,E,D} Named after Sir William Rowan Hamilton (1805 –1865) The Knight’s Tour problem is a Hamiltonian cycle problem Prof. Amr Goneid, AUC 10 A F G B E D C Icosian Game

Hamiltonian Cycle DemoA Hamiltonian Cycle is a cycle that visits each node exactly once. Here we show a Hamiltonian cycle on a 5-dimensional hypercube. It starts by completely traversing the 4-dimensional hypercube on the left before reversing the traversal on the right subcube. Hamiltonian cycles on hypercubes provide constructions for Gray codes: orderings of all subsets of n items such that neighboring subsets differ in exactly one element. Hamilitonian cycle is an NP-complete problem, so no worst-case efficient algorithm exists to find such a cycle. In practice, we can find Hamiltonian cycles in modest-sized graphs by using backtracking with clever pruning to reduce the search space. Prof. Amr Goneid, AUC 11

Hamiltonian Cycle Demo http://www.cs.sunysb.edu/~skiena/combinatorica/animations/ham.html Hamiltonian Cycle Demo Prof. Amr Goneid, AUC 12

Euler Circuit Leonhard Euler Konigsberg Bridges (1736) (not Eulerian ) Euler Circuit: A cycle that includes every edge once. Used in bioinformatics to reconstruct the DNA sequence from its fragments Prof. Amr Goneid, AUC 13

Euler Circuit DemoAn Euler circuit in a graph is a traversal of all the edges of the graph that visits each edge exactly once before returning home. A graph has an Euler circuit if and only if all its vertices are that of even degrees. It is amusing to watch as the Euler circuit finds a way back home to a seemingly blocked off start vertex. We are allowed (indeed required) to visit vertices multiple times in an Eulerian cycle, but not edges. Prof. Amr Goneid, AUC 14

Euler Circuit Demohttp://www.cs.sunysb.edu/~skiena/combinatorica/animations/euler.htmlEuler Circuit DemoProf. Amr Goneid, AUC 15

3. Connectivity Connected Graph: An undirected graph with a path from every vertex to every other vertex A Disconnected Graph may have several connected components Tree: A connected Acyclic graph Prof. Amr Goneid, AUC 16 A F G B E D C A F G B E D C

Connected Components DemoWhat happens when you start with an empty graph and add random edges between vertices? As you add more and more edges, the number of connected components in the graph can be expected to drop, until finally the graph is connected. An important result from the theory of random graphs states that such graphs very quickly develop a single ``giant'' component which eventually absorbs all the vertices. Prof. Amr Goneid, AUC 17

Connected Components Demohttp://www.cs.sunysb.edu/~skiena/combinatorica/animations/concomp.html Randomly Connected Graph Demo Prof. Amr Goneid, AUC 18

Connectivity Articulation Vertex: if removed with all of its edges will cause a connected graph to be disconnected, e.g., G and D are articulation vertices Prof. Amr Goneid, AUC 19 A F B E D C A F G B E D C

ConnectivityDegree Of a vertex, the number of edges connected to it. Degree Of a graph, the maximum degree of any vertex (e.g. B has degree 2, graph has degree 3). In a connected graph the sum of the degrees is twice the number of edges, i.e Prof. Amr Goneid, AUC 20 A F G B E D C

Connectivity In-Degree/Out-Degree: the number of edges coming into/emerging from a vertex in a connected graph (e.g.G has in-degree 3 and out-degree 1). Prof. Amr Goneid, AUC 21 A F G B E D C

ConnectivityComplete Graph:There is an edge between every vertex and everyother vertex. In this case,the number of edges is maximum: Notice that the minimum number of edges for a connected graph ( a tree in this case) is (V-1) Prof. Amr Goneid, AUC 22 A D C B

Density (of edges)Density of a Graph: Dense Graph:Number of edges is close to Emax = V(V-1)/2. So, E = O(V 2 ) and D is close to 1 Sparse Graph: Number of edges is close to E min = (V-1). So, E = O(V) Prof. Amr Goneid, AUC 23

4. Other Properties Planar Graph: A graph that can be drawn in the plain without edges crossing Prof. Amr Goneid, AUC 24 A D C B A D C B Non-Planar

Other PropertiesGraph Coloring:To assign color (or any distinctive mark) to verticessuch that no two adjacent vertices have the same color. The minimum number of colors needed is called theChromatic Order of the graph (G) . For a complete graph, (G) = V. Prof. Amr Goneid, AUC 25 1 2 3 4

Other PropertiesAn Application:Find the number of exam slots for 5 courses. If a single student attends two courses, an edge exists between them. Slot Courses 1 (red) CS 2 (Green) EE, Econ, Phys 3 (Blue) Math Prof. Amr Goneid, AUC 26 EE Math CS Phys Econ

5. RepresentationAdjacency Matrix:V x V Matrix a( i,j)a(i,j ) = 1 if vertices ( i ) and (j) are adjacent, zero otherwise. Usually self loops are not allowed so that a( i,i ) = 0. For undirected graphs, a( i,j ) = a( j,i ) Matrix is symmetric Prof. Amr Goneid, AUC 27 A D C B A B C D A 0 1 1 0 B 1 0 1 0 C 1 1 0 1 D 0 0 1 0

Adjacency MatrixFor weighted undirected graphs, a(i,j) = a(j,i ) = w ij (symmetric) Prof. Amr Goneid, AUC 28 A B C D A 0 2 1 0 B 2 0 3 0 C 1 3 0 5 D 0 0 5 0 A D C B 2 3 1 5

Adjacency MatrixFor weighted directed graphs, a( i,j) ≠ a(j,i) Matrix is not symmetric Prof. Amr Goneid, AUC 29 A B C D A 0 2 1 0 B 0 0 0 0 C 0 3 0 5 D 0 0 0 0 A D C B 2 3 1 5

RepresentationThe adjacency matrix is appropriate for dense graphsbut not compact for sparse graphs. e.g., for a lattice graph, the degreeof a vertex is 4, so that E = 4V. Number of “1’s” to total matrix sizeis approximately 2 E / V 2 = 8 / V. For V >> 8, the matrix is dominated by zeros. Prof. Amr Goneid, AUC 30

RepresentationAdjacency List:An array of vertices with pointers to linked lists of adjacent nodes, e.g., The size is O(E + V) so it is compact for sparse graphs. Prof. Amr Goneid, AUC 31 A D C B C B D B A C A C A B D C

Edge ListAn edge (u,v,w) existing between nodes (u) and (v) with weight (w) is modeled as a structure (Edge). Space requirements is O(E) Prof. Amr Goneid, AUC 32 A D C B 2 3 1 5 u A A B C v B C C D w 2 1 3 5

6. Examples of Graph AlgorithmsExamples of Graph Algorithms:Graph TraversalShortest Paths Minimum Cost Spanning TreesProf. Amr Goneid, AUC 33

6.1 Graph TraversalDepth-First Search (DFS) Visits every node in the graph by following node connections in depth.Recursive algorithm.An Array val[v] records the order in which vertices are visited. Initialized to “unseen”.Any edge to a vertex that has not been seen is followed via the recursive call. Prof. Amr Goneid, AUC 34

DFS Algorithm Prof. Amr Goneid, AUC35 // Assume No. of vertices = v and order = 0 initially // val[1..v] is an array recording the visit order ALGORITHM DFS( ) { // Initialize all to unseen for each vertex k set val[k] = unseen; // Follow Nodes in Depth for each vertex k if (val[k] == unseen) Visit(k); }

Algorithm // Assume order = 0 initiallyvoid DFS() { int k; int unseen = -2; // Initialize all to unseen for (k = 1; k <= v; k++) val [k] = unseen; // Follow Nodes in Depth for (k = 1; k <= v; k++) if ( val [k] == unseen) visit(k); } Prof. Amr Goneid, AUC 36

DFS Algorithm (continued) Prof. Amr Goneid, AUC 37 //Assume an adjacency matrix a[1..v][1..v] ALGORITHM Visit(k) { val [k] = ++order; for each vertex (t), t =1..v if (vertices (t) and (k) are adjacent) if ( val [t] == unseen) Visit(t); }

Algorithm (continued)void visit(int k){ int t; val [k] = ++order; for (t = 1; t <= v; t++) if (a[k][t] != 0) if ( val [t] == unseen) visit(t); } Prof. Amr Goneid, AUC 38

Example Prof. Amr Goneid, AUC 39 A B C G F D E 1 VAL[K] A B C D E F G 1 start

Example Prof. Amr Goneid, AUC 40 A B C G F D E 1 2 VAL[K] A B C D E F G 1 2

Example Prof. Amr Goneid, AUC 41 A B C G F D E 1 2 3 VAL[K] A B C D E F G 1 2 3

Example Prof. Amr Goneid, AUC 42 A B C G F D E 1 2 3 VAL[K] A B C D E F G 1 2 3 4 4

Example Prof. Amr Goneid, AUC 43 A B C G F D E 1 2 3 VAL[K] A B C D E F G 1 2 3 5 4 4 5

Example Prof. Amr Goneid, AUC 44 A B C G F D E 1 2 3 VAL[K] A B C D E F G 1 2 3 5 6 4 4 5 6

Example Prof. Amr Goneid, AUC 45 A B C G F D E 1 2 3 VAL[K] A B C D E F G 1 2 3 5 6 4 7 4 5 6 7

ExercisesShow how DFS can be used to determine the number of connected components in a graph. Explore the non-recursive version of the DFS using a stack. What will happen if you use a queue instead of the stack?Prof. Amr Goneid, AUC 46

Non-Recursive DFS Prof. Amr Goneid, AUC47 // Assume hold = node discovered but not yet visited // Val[1..v ] is set to “unseen” initially , order = 0 // Assume a stack, initially empty ALGORITHM DFS(k ) { push (k) on top of stack; while stack is not empty { pop stack top into (k); val [k] = ++order; for vertices t = v..1 // Scan from right to left if (vertices (t) and (k) are adjacent)) if ( val [t] == unseen) { push (t) on top of stack; val [t] = hold;} } }

Non-Recursive DFS // Assume unseen = -2 and hold = -1// Val[ ] is set to “unseen” initially , order = 0 Stact < int > S; // A stack of integers void DFS( int k) { int t; S.push (k); while (! S.stackIsEmpty ()) { S.pop (k); val [k] = ++order; for (t = v; t >= 1; t--) // Scan from right to left if (a[k][t] != 0) if ( val [t] == unseen) { S.push (t); val [t] = hold;} } } Prof. Amr Goneid, AUC48

Breadth-First Search (BFS) Prof. Amr Goneid, AUC49 // Replacing the stack by a queue, gives the BFS algorithm // Also an Exhaustive Search Algorithm // Assume a queue, intially empty ALGORITHM BFS(k ) { add (k) to end of queue; while queue is not empty { remove queue front into (k); val [k] = ++order; for vertices t = 1..v // Scan from left to right if (vertices (t) and (k) are adjacent)) if ( val [t] == unseen) { add (t) to end of queue; val [t] = hold;} } }

Breadth-First Search (BFS) // Replacing the stack by a queue, gives the BFS algorithm Queuet < int > Q; // A queue of integers void BFS( int k) { int t; Q.enqueue (k); while (! Q.queueIsEmpty ()) { Q.dequeue (k); val [k] = ++order; for (t = 1; t <= v; t++) // Scan from left to right if (a[k][t] != 0) if ( val [t] == unseen) { Q.enqueue (t); val [t] = hold;} } }Prof. Amr Goneid, AUC 50

Example BFS Prof. Amr Goneid, AUC 51 A B C G F D E 1 2 3 4 6 7 5 A B C D E F G 1 5 3 6 7 4 5

DFS and BFS of a TreeFor a tree structure:DFS is equivalent to Pre-order traversalBFS is equivalent to Level-order traversal DFS Demo BFS Demo Prof. Amr Goneid, AUC 52

Graph Traversal Demoshttp://www.cs.sunysb.edu/~skiena/combinatorica/animations/search.htmlProf. Amr Goneid, AUC 53

ExerciseModel the shown maze as a graph. Show how DFS can be used to find the exit. Prof. Amr Goneid, AUC 54 in out

8. A Simple Graph Class To represent a weighted undirected graph with a maximum of Vmax vertices and Emax = Vmax(Vmax -1)/2 edges. The verices are numbered 0,1,...V-1. The graph is assumed to be on a text file in the form of an adjacency matrix. The weights on the edges are assumed to be positive integers with zero weight indicating the absence of an edge. When loaded from the text file, the weights are stored in a 2-D array ( AdjMatrix ) representing the adjacency matrix. Another array (edges) stores the non-zero edges in the graph. An edge ( u,v,w ) existing between nodes (u) and (v) with weight (w) is modeled as a class (Edge). Prof. Amr Goneid, AUC 55

Edge Class// File: Edge.h // Definition of Edge class# ifndef EDGE_H #define EDGE_H typedef int weightType ; // weights are positive integers class Edge { public: int u,v ; weightType w; bool operator < (const Edge &e) { return (w < e.w ); } bool operator <= (const Edge &e) { return (w <= e.w ); } }; // end of class Edge declaration #endif // EDGE_H Prof. Amr Goneid, AUC 56

Graph Class// File: Graphs.h // Graph library header file#ifndef GRAPHS_H#define GRAPHS_H #include <string> #include " Edge.h " using namespace std ; const int Vmax = 50; // Maximum number of vertices const int Emax = Vmax*(Vmax-1)/2; // Maximum number of edges Prof. Amr Goneid, AUC57

Graph Classclass Graphs{ public: Graphs(); // Constructor ~Graphs(); // Destructor // Map vertex number to a name (character) char Vname ( const int s) const ; void getGraph(string fname); // Get Graph from text File ( fname) void dispGraph( ) const; // Display Ajacency Matrix int No_of_Verices( ) const; // Get number of vertices (V) int No_of_Edges( ) const; // Get Number of Non-zero edges (E) void dispEdges( ) const ; // Display Graph edges void DFS( ); // Depth First Search Traversal (DFS) Prof. Amr Goneid, AUC 58

Graph Classprivate: int V, E; // No.of vertices (V) and edges (E) weightType AdjMatrix [ Vmax ][ Vmax ]; // Adjacency Matrix Edge edges[ Emax ]; // Array of non-zero edges int order; // Order of Visit of a node in the DFS int val[Vmax]; // Array holding order of traversal void getEdges(); // Get edges from adjacency matrix void printEdge(Edge e) const; // Output an edge (e) void visit(int k); // Node Visit Function for DFS};# endif // GRAPHS_H #include "Graphs.cpp"Prof. Amr Goneid, AUC 59

6.2 Shortest Paths(General)In a graph G(V,E), find shortest paths from a single source vertex (S) to all other vertices. Edsger Dijkstra published an algorithm to solve this problem in 1959. Prof. Amr Goneid, AUC 60

Shortest Paths: Dijkstra’s Algorithm Dijkstra’s Algorithm for V vertices: Uses three arrays: - Distance[ i ]: holds distance from (S) to vertex ( i ). - Processed[ i ]: to flag processed vertices. - Via[ i]: holds index of vertex from which we can directly reach vertex ( i). Prof. Amr Goneid, AUC 61

Initialization Prof. Amr Goneid, AUC 62

MethodProf. Amr Goneid, AUC63 Update Rule:New D si = min {D si , D sj + w ij } S z y x j i Already Processed D sj D si W ij not yet processed closest to S adjacent to j

Dijkstra’s Algorithm Repeat Find j = index of unprocessed node closest to (S) Mark (j) as now processed For each node ( i ) not yet processed : if ( i ) is adjacent to (j) then { new_distance = Distance[j] + W ij if new_distance < Distance[ i ] then { Distance[ i ] = new_distance ; Via[i ] = j ; } }Until all vertices are processed Prof. Amr Goneid, AUC 64

ExampleInitial Source = A Prof. Amr Goneid, AUC 65 A E C B D 20 15 40 35 10 35 A 0 A A 0 No No No No yes 20  35 15 0 Dist Processed Via A B C D E

Examplej = B Prof. Amr Goneid, AUC 66 A E C B D 20 15 40 35 10 35 A 0 A A 0 No No No yes yes 20  35 15 0 Dist Processed Via A B C D E

Examplej = B i = D Prof. Amr Goneid, AUC 67 A E C B D 20 15 40 35 10 35 A B A A 0 No No No yes yes 20 55 35 15 0 Dist Processed Via A B C D E

Examplej = E Prof. Amr Goneid, AUC 68 A E C B D 20 15 40 35 10 35 A B A A 0 yes No No yes yes 20 55 35 15 0 Dist Processed Via A B C D E

Examplej = E i = C Prof. Amr Goneid, AUC 69 A E C B D 20 15 40 35 10 35 A B E A 0 yes No No yes yes 20 55 30 15 0 Dist Processed Via A B C D E

Examplej = C Prof. Amr Goneid, AUC 70 A E C B D 20 15 40 35 10 35 A B E A 0 yes No yes yes yes 20 55 30 15 0 Dist Processed Via A B C D E

Examplej = C i = D Prof. Amr Goneid, AUC 71 A E C B D 20 15 40 35 10 35 A B E A 0 yes No yes yes yes 20 55 30 15 0 Dist Processed Via A B C D E

Examplej = D Prof. Amr Goneid, AUC 72 A E C B D 20 15 40 35 10 35 A B E A 0 yes yes yes yes yes 20 55 30 15 0 Dist Processed Via A B C D E

ExampleFinal Prof. Amr Goneid, AUC 73 A E C B D 20 15 40 35 10 35 A B E A 0 yes yes yes yes yes 20 55 30 15 0 Dist Processed Via A B C D E A→B A→E→C A→B→D A→E

How to print the path?/* The function Vname (k) maps a vertex number to a name (e.g a character A, B,.. etc). Given the via[ ] array resulting from Dijkstra’s algorithm, the following recursive function prints the vertices on the shortest path from source (s) to destination ( i ). */ void Graphs:: printPath ( int s, int i ) const { if ( i == s) cout << Vname (s); else { printPath(s,via[ i]); cout << Vname(i); } } Prof. Amr Goneid, AUC 74

Demohttp://www.cs.sunysb.edu/~skiena/combinatorica/animations/dijkstra.html Shortest Paths Demo1 Shortest Paths Demo2 Prof. Amr Goneid, AUC 75

6.3 Minimum Cost Spanning Trees (a) Spanning Tree Consider a connected undirected graph G(V,E). A sub-graph S(V,T) is a spanning tree of the graph (G) if:V(S) = V(G) and T  ES is a tree, i.e., S is connected and has no cycles Prof. Amr Goneid, AUC 76

Spanning Tree Prof. Amr Goneid, AUC 77 S(V,T): V = {A,B,C,D,E,F,G} T = {AB,AF,CD,DE,EF,FG} F E G A D C B

Spanning Tree Prof. Amr Goneid, AUC 78 Notice that: |T| = |V| - 1 and adding any edge ( u,v ) T will produce a cycle so that S is no longer a spanning tree (e.g. adding (G,D)) F E G A D C B

One Graph, Several Spanning Trees Prof. Amr Goneid, AUC 79

Spanning Forest Prof. Amr Goneid, AUC 80 For a connected undirected graph G(V,E), a spanning forest is S(V,T) if S has no cycles and T E. S(V,T) will be composed of trees (V 1 ,T 1 ), (V 2 ,T 2 ), …, ( V k ,T k ), k ≤ |V| F E G A D C B

(b) Minimum Cost Spanning Tree (MST)Consider houses A..F connected bymuddy roads with the distances indicated. We want to pave someroads such that:We can reach a house fromany other house via paved roads. The cost of paving is minimum.This problem is an example of finding a Minimum Spanning Tree (MST) Prof. Amr Goneid, AUC 81 E G A B C F D 4 4 9 2 3 3 7 6 4 5

Minimum Spanning Tree (MST)Cost: For a weighted graph, the cost of a spanning tree is the sum of the weights of the edges in that tree.Minimum Spanning tree: A spanning tree of minimum costFor the shown graph, the minimum cost is 22 Prof. Amr Goneid, AUC 82 E G A B C F D 4 4 9 2 3 3 7 6 4 5

Kruskal’s Algorithm for MSTThe algorithm was written by Joseph Kruskal in 1956 A Greedy Algorithm: Builds the MST edge by edge into a set of edges (T). At a given stage, chooses an edge that results in minimum increase in the sum of costs included so far in (T). The set (T) might not be a tree at all stages of the algorithm, but it can be completed into a tree iff there are no cycles in (T). Prof. Amr Goneid, AUC 83

Kruskal’s Algorithm for MSTBuilds up forests , then joins them in a single tree. Constraints: - The graph must be connected. - Uses exactly V-1 edges. - Excludes edges that form a cycle. Prof. Amr Goneid, AUC 84

Abstract Algorithm-Form Set E of edges in increasing order of costs. - Set MST T = empty -RepeatSelect an edge (e) from top. Delete edge from E set. Add edge (e) to (T) if it does not form a cycle, otherwise, reject. -Until we have V-1 successful edges. Prof. Amr Goneid, AUC 85

Example Edge u v w accept 1 E F 2 2 A C 3 3 C E 3 4 A E 4 5 C D 4 6 E G 4 7 D F 5 8 B D 6 Prof. Amr Goneid, AUC 86 E G A B C F D 4 4 9 2 3 3 7 6 4 5

Example Edge u v w accept 1 E F 2 yes 2 A C 3 3 C E 3 4 A E 4 5 C D 4 6 E G 4 7 D F 5 8 B D 6 Prof. Amr Goneid, AUC 87 E G A B C F D 4 4 9 2 3 3 7 6 4 5

Example Edge u v w accept 1 E F 2 yes 2 A C 3 yes 3 C E 3 4 A E 4 5 C D 4 6 E G 4 7 D F 5 8 B D 6 Prof. Amr Goneid, AUC 88 E G A B C F D 4 4 9 2 3 3 7 6 4 5

Example Edge u v w accept 1 E F 2 yes 2 A C 3 yes 3 C E 3 yes 4 A E 4 5 C D 4 6 E G 4 7 D F 5 8 B D 6 Prof. Amr Goneid, AUC 89 E G A B C F D 4 4 9 2 3 3 7 6 4 5

Example Edge u v w accept 1 E F 2 yes 2 A C 3 yes 3 C E 3 yes 4 A E 4 NO 5 C D 4 6 E G 4 7 D F 5 8 B D 6 Prof. Amr Goneid, AUC 90 E G A B C F D 4 4 9 2 3 3 7 6 4 5

Example Edge u v w accept 1 E F 2 yes 2 A C 3 yes 3 C E 3 yes 4 A E 4 NO 5 C D 4 yes 6 E G 4 7 D F 5 8 B D 6 Prof. Amr Goneid, AUC 91 E G A B C F D 4 4 9 2 3 3 7 6 4 5

Example Edge u v w accept 1 E F 2 yes 2 A C 3 yes 3 C E 3 yes 4 A E 4 NO 5 C D 4 yes 6 E G 4 yes 7 D F 5 8 B D 6 Prof. Amr Goneid, AUC 92 E G A B C F D 4 4 9 2 3 3 7 6 4 5

Example Edge u v w accept 1 E F 2 yes 2 A C 3 yes 3 C E 3 yes 4 A E 4 NO 5 C D 4 yes 6 E G 4 yes 7 D F 5 NO 8 B D 6 Prof. Amr Goneid, AUC 93 E G A B C F D 4 4 9 2 3 3 7 6 4 5

Example Edge u v w accept 1 E F 2 yes 2 A C 3 yes 3 C E 3 yes 4 A E 4 NO 5 C D 4 yes 6 E G 4 yes 7 D F 5 NO 8 B D 6 yes Prof. Amr Goneid, AUC 94 E G A B C F D 4 4 9 2 3 3 7 6 4 5

How to test for Cycles?Simple Union- Simple Find Given a set (1,2,..,n} initially partitioned into ndisjoint sets (each element is its own parent):Find (i ): return the sub-set that (i) is in (i.e. return the parent set of i ). Union ( k,j ): combine the two sub-sets that (j) and (k) are in (make k the child of j) Union builds up a tree, while find will search for the root of the tree. Cost is O(log n) Prof. Amr Goneid, AUC 95

How to test for Cycles?Represent vertices as Disjoint SetsInitially, each node is its own parent (-1) A B C D E F G 1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 Prof. Amr Goneid, AUC 96 A B C D E F G Let the parent set of u be p(u)

How to test for Cycles?When edge (E,F) is selected, we find that p(E)  p(F). So, we make a union between p(E) and p(F), i.e., p(F) becomes the child of p(E). Same for edge (A,C) A B C D E F G 1 2 3 4 5 6 7 -1 -1 1 -1 -1 5 -1 Prof. Amr Goneid, AUC 97 A B C D E F G Parent table after selecting (E,F) then (A,C)

How to test for Cycles?When (C,E) is selected, we find that P(C)  P(E). This means that the edge will be accepted.Select (A,E), Find will give P(A) = P(E) (cycle, reject). A B C D E F G 1 2 3 4 5 6 7 -1 -1 1 -1 3 5 -1 Prof. Amr Goneid, AUC 98 A B C D E F G Parent table after accepting (E,F) then (A,C), then (C,E), then rejecting (A,E)

How to test for Cycles?When (C,D) is selected, we find that P(C)  P(D). This means that the edge will be accepted.Select (E,G), Find will give P(E)  P(G) (accept). A B C D E F G 1 2 3 4 5 6 7 -1 -1 1 3 3 5 5 Prof. Amr Goneid, AUC 99 A B C D E F G Parent table after 5 th accepted edge

How to test for Cycles?When (D,F) is selected, we find that P(D) = P(F). This means that the edge will be rejected.Select (B,D), Find will give P(B)  P(D) (accept). A B C D E F G 1 2 3 4 5 6 7 -1 4 1 3 3 5 5 Prof. Amr Goneid, AUC 100 A B C D E F G Parent table after 6 th accepted edge (Final MST)

Kruskal’s AlgorithmInsert edges with weights into a minimum heapPut each vertex in a separate set i = 0 ; While (( i < V-1) && (heap not empty)) { Remove edge ( u,v ) from heap Find set (j) of connected vertices having (u) Find set (k) of connected vertices having (v) if ( j != k ) { i ++; MST [ i ].u = u; MST [ i ].v = v; MST [ i].w = w; Make a Union between set (j) and set (k); } } Prof. Amr Goneid, AUC 101

Kruskal’s Algorithm Demo http://www.cs.sunysb.edu/~skiena/combinatorica/animations/mst.html Kruskal's algorithm at work on a graph of distances between 128 North American cities. Almost imperceptively at first, short edges get added all around the continent, slowly building forests until the tree is completed. MST (Kruskal ) Demo1 MST ( Kruskal ) Demo2 Prof. Amr Goneid, AUC 102

Learn on your own about:Directed Graphs or DigraphsEdge-List representation of graphs Prim’s algorithm for Minimum Spanning Trees. The algorithm was developed in 1930 by Czech mathematician Vojtěch Jarník and later independently by computer scientist Robert C. Prim in 1957 and rediscovered by Edsger Dijkstra in 1959. Therefore it is also sometimes called the DJP algorithm, the Jarník algorithm, or the Prim– Jarník algorithm. Prof. Amr Goneid, AUC 103