Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 3 Graphs.

Similar presentations


Presentation on theme: "Unit 3 Graphs."— Presentation transcript:

1 Unit 3 Graphs

2 Graphs – Basic Concepts
Basic definitions: vertices and edges More definitions: paths, simple paths, cycles, loops Graph representation Adjacency matrix Adjacency lists

3 Graph Definition: A graph is a collection (nonempty set) of vertices and edges Vertices: can have names and properties Edges: can be directed Adjacent vertices: there is an edge between them

4 Directed and undirected graphs
B C D These are two different graphs

5 1 2 1 2 3 4 5 6 3 G1 G2 1 2 G3 3 Undirected graph 2 degree 3 3 3 3 3 1
Undirected graph 2 degree 1 2 3 1 2 3 3 3 3 4 5 6 3 3 G1 1 1 G2 1 1 in:1, out: 1 Directed graph 1 in: 1, out: 2 in-degree out-degree 2 in: 1, out: 0 G3 CHAPTER 6

6 More definitions : Cycle
Simple path with distinct edges, except that the first vertex is equal to the last A B C D A B C A B A C B C B A C A graph without cycles is called acyclic graph.

7 More definitions : Loop
An edge that connects the vertex with itself A B C D

8 Graphs and Trees A C E B D C A E B D Tree1: root A Tree2: root C

9 A spanning tree of an undirected graph
A sub-graph that contains all the vertices, and no cycles. If we add any edge to the spanning tree, it forms a cycle, and the tree becomes a graph A B C D A B graph spanning tree C D

10 Examples A B C D All spanning trees of the graph on the previous slide

11 Complete graphs Graphs with all edges present – each vertex is connected to all other vertices A B C D E Dense graphs: relatively few of the possible edges are missing Sparse graphs: relatively few of the possible edges are present A complete graph

12 Graph Representation Adjacency Matrix Adjacency Lists
Linked Adjacency Lists Array Adjacency Lists

13 Adjacency Matrix n x n matrix, where n = No of vertices
A[i,j] = 1 iff (i,j) is an edge 1 2 3 4 5 2 3 1 4 5 1 1 1 1 1 1 1 1 1 1

14 Adjacency Matrix Properties
2 3 1 4 5 Diagonal entries are zero. Adjacency matrix of an undirected graph is symmetric. A(i,j) = A(j,i) for all i and j.

15 Adjacency Matrix (Digraph)
2 3 1 4 5 Diagonal entries are zero. Adjacency matrix of a digraph need not be symmetric.

16 Adjacency Matrix n2 bits of space
O(n) time to find vertex degree and/or vertices adjacent to a given vertex. O(1) time to determine if there is an edge between two given vertices.

17 Adjacency Lists Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. An array of n adjacency lists. aList[1] = (2,4) aList[2] = (1,5) aList[3] = (5) aList[4] = (5,1) aList[5] = (2,4,3) 2 3 1 4 5

18 Weighted Graphs Cost adjacency matrix. C(i,j) = cost of edge (i,j)
Adjacency lists => each list element is a pair (adjacent vertex, edge weight)

19 Linked Adjacency Lists
Each adjacency list is a chain. 2 3 1 4 5 aList[1] aList[5] [2] [3] [4] 2 4 1 5 5 5 1 2 4 3 Array length n simply means we need an array with n spots. A direct implementation using a Java array would need n+1 spots, because spot 0 would not be utilized. However, by using spot 0 for vertex 1, spot 1 for vertex 2, and so on, we could get by with a Java array whose length is actually n. Array Length = n # of chain nodes = 2e (undirected graph) # of chain nodes = e (digraph)

20 1 2 3 4 5 6 7 1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 7 1 2 2 3 3 1 3 3 1 2 1 2 5 G1 4 6 1 2 1 5 7 2 1 6 G4 G3 2 An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes

21 Graph Traversals DFT : Depth-First Traversal
BFT : Breadth-First Traversal Lecture 7: Graphs

22 Depth-First Traversal
Use a stack (or recursion) to store set of vertices to be visited From a given vertex, visit neighbors I+1 … N only after traversing all possible edges from neighbor I Lecture 7: Graphs

23 Depth-First Traversal
2 1 4 6 3 5 Lecture 7: Graphs

24 DFS (Non Recursive) for(j=0; j<n ; j++) {
if(visited[j]!=1 && G[i][j]==1) s.push(j); } }//end of for }//end of if }//end of while }//end of function void dfs_nr(int i) { stack s, int j; s.push(i); while(!s.empty()) i=s.pop(); if(visited[i]!=1) cout<<i; visited[i]=1;

25 Recursive DFS void DFT(int i) { int j; cout<< i; visited[i]=1; for(j=0;j<n;j++) if (!visited[j] && G[i][j]==1) DFT(j); }

26 Depth-First Search pseudo code
Algorithm DFS(i) Input: A vertex i in a graph. Output: Traversing of graph is recorded. Initialize stack S Push i in the stack loop(stack not empt) i <-pop element from stack if (! Visited[i]) 1. set visited[i] to 1 and display it. 2. loop for each w adj. to I 1. if (!visited[w]) 1. push w in stack 2. end if 3. end loop 3. end if End loop End of function

27 BFT non recursive { if(visited[j]!=1 && G[i][j]==i) visited[j]=1; cout<< j; Q.addq(j); }//end if }//end for loop }//end while void BFT(int i) { Queue Q; cout<< i; Q.addq(i); visited[i]=1; while(!Q.empty()) i=Q.deleteq(); for(j=0; j<=n; j++)

28 Prim’s Algorithm Aim: To find a minimum spanning tree T
Step 1: Select any node to be the first node of T. Step 2: Consider the arcs which connect nodes in T to nodes outside T. Pick the one with minimum weight. Add this arc and the extra node to T. (If there are two or more arcs of minimum weight, choose any one of them.) Step 3: Repeat Step 2 until T contains every node of the graph.

29 Complete Graph 4 B C 4 2 1 A 4 E F 1 2 D 3 10 5 G 5 6 3 4 I H 3 2 J

30 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

31 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

32 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

33 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

34 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

35 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

36 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

37 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

38 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

39 Old Graph New Graph 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1 A 4 E F 1 2 D 3
10 D 3 5 G 10 5 G 5 6 3 5 6 3 4 I 4 H I H 3 2 J 3 2 J

40 Complete Graph Minimum Spanning Tree 4 B C 4 4 B C 2 1 4 2 1 A 4 E F 1
D 3 2 10 D 5 G G 5 6 3 3 4 I H I H 3 2 J 3 2 J

41 Prim’s Algorithm int GraphMatrix :: prim(GraphMatrix &spanning) {
int cost[MAX][MAX], dist[MAX], source[MAX], visited[MAX]; int u, v, mindist, edges, i, min_cost, j; for(i = 0; i < num; i ++)// creation of cost matrix for(j = 0; j < num; j ++) if(array[i][j] == 0) cost[i][j] = INFINITY; else cost[i][j] = array[i][j]; spanning.array[i][j] = 0; }

42 dist[0] = 0; visited[0] = 1; for(i = 1; i < num; i ++) { dist[i] = cost[0][i]; source[i] = 0; visited[i] = 0; } min_cost = 0; edges = num - 1;

43 while(edges > 0) { mindist = INFINITY; for(i = 1; i < num; i ++) if(visited[i] == 0 && dist[i] < mindist) v = i; mindist = dist[i]; } u = source[v]; spanning.array[u][v] = dist[v]; spanning.array[v][u] = dist[v]; edges --; visited[v] = 1; for(i = 1; i < num; i ++) if(visited[i] == 0 && cost[i][v] < dist[i]) { dist[i] = cost[i][v]; source[i] = v; } min_cost = min_cost + cost[u][v]; spanning.num = num; return(min_cost);

44 Analysis of Prim's Algorithm
Running Time = O(n*n)

45 Kruskal’s Algorithm Aim: To find a minimum spanning tree for a connected graph with n nodes: Step 1: Choose the arc of least weight. Step 2: Choose from those arcs remaining the arc of least weight which does not form a cycle with already chosen arcs. (If there are several such arcs, choose one arbitrarily.) Step 3: Repeat Step 2 until n – 1 arcs have been chosen.

46 Complete Graph 4 B C 4 2 1 A 4 E F 1 2 D 3 10 5 G 5 6 3 4 I H 3 2 J

47 4 1 A B A D 4 4 B C B D 4 10 2 B C B J C E 4 2 1 1 5 C F D H A 4 E F 1 6 2 2 D J E G D 3 10 5 G 3 5 F G F I 5 6 3 4 3 4 I G I G J H 3 2 J 2 3 H J I J

48 Sort Edges (in reality they are placed in a priority queue - not sorted - but sorting them makes the algorithm easier to visualize) 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

49 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

50 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

51 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

52 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

53 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

54 Cycle Don’t Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3
I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

55 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

56 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

57 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

58 Cycle Don’t Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3
I I J A 4 E F 1 4 4 2 A B B D D 3 10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

59 Add Edge 1 1 A D C F 2 2 C E E G 4 2 3 B C H J F G 4 2 1 3 3 G I I J A
10 5 G 4 4 B C G J 5 6 3 4 5 5 I F I D H H 3 2 J 6 10 D J B J

60 Minimum Spanning Tree Complete Graph 4 B C 4 B C 4 4 2 1 2 1 A E A 4 F
D 2 D 3 10 G 5 G 3 5 6 3 4 I I H H 3 2 3 J 2 J

61 1 1 2 2 3 5 4 6 6 4 4 3 5 8 6 7 4 3 7

62 STEP EDGE Considered Connected Components
Initialization {1} {2} {3} {4} {5} {6} {7} {1,2} {1,2} {3} {4} {5} {6} {7} {2,3} {1, 2, 3} {4} {5} {6} {7} {4,5} {1, 2, 3} {4, 5} {6} {7} {6,7} {1, 2, 3} {4, 5} {6,7} {1,4} {1, 2, 3,4, 5} {6,7} {2,5} rejected {4,7} {1, 2, 3,4, 5,6,7}

63 Vertex no  Component no After adding {1,2} Vertex no  Component no

64 After adding {2,3} Vertex no  Component no After adding {4,5} Vertex no  Component no

65 Kruskal Algorithm Kruskal (G =<N,A>: graph; length: A R+ ): set of edges {initialization} Sort A by increasing length n the no of nodes T  {will contain the edges of the minimum Spanning Tree } Loop(T contains n-1 edges) e  {u,v}  shortest edge not yet consider ucomp  find(u) vcomp  find(v) if (ucomp !=vcomp) then merge (ucomp,vcomp) T  T U {e} End Loop return T

66 Analysis of Kruskal's Algorithm
Sorting edges  O(e lg e) Spanning tree iteration , this involves merging of two components O(lg n) and merging edges. O(e lg n) Overall  O(e lg e) + O(e lg n)

67 Shortest path Dijkstra(L[1..n,1..n]) Array D[2..n] { initialization} c{2,3..n} Loop i 2 to n do D[i]L[1,i] Repeat n-2 times V some element in C which is minimized D[v] C C\{v} //remove it from C For each w belongs to C do D[w] min(D[w],D[v]+L[v,w]) Return D End .


Download ppt "Unit 3 Graphs."

Similar presentations


Ads by Google