Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.

Similar presentations


Presentation on theme: "CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA."— Presentation transcript:

1 CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO ORD DFW SFO LAX 802 1743 1843 1233 337

2 GRAPH ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

3 EDGE & GRAPH TYPES ORDDFW flight AA 1206 ORDDFW flight route 802 miles

4 APPLICATIONS Electronic circuits Printed circuit board Integrated circuit Transportation networks Highway network Flight network Computer networks Local area network Internet Web Databases Entity-relationship diagram

5 TERMINOLOGY XU V W Z Y a c b e d f g h i j

6 X V W Z Y b e d f g h i j

7 P1P1 XU V W Z Y a c b e d f g hP2P2

8 C1C1 XU V W Z Y a c b e d f g hC2C2

9 EXERCISE ON TERMINOLOGY 1. Number of vertices? 2. Number of edges? 3. What type of the graph is it? 4. Show the end vertices of the edge with largest weight 5. Show the vertices of smallest degree and largest degree 6. Show the edges incident to the vertices in the above question 7. Identify the shortest simple path from HNL to PVD 8. Identify the simple cycle with the most edges ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

10 EXERCISE PROPERTIES OF UNDIRECTED GRAPHS A graph with given number of vertices (4) and maximum number of edges

11 EXERCISE PROPERTIES OF UNDIRECTED GRAPHS A graph with given number of vertices (4) and maximum number of edges

12 EXERCISE PROPERTIES OF DIRECTED GRAPHS A graph with given number of vertices (4) and maximum number of edges

13 EXERCISE PROPERTIES OF DIRECTED GRAPHS A graph with given number of vertices (4) and maximum number of edges

14 SUBGRAPHS Subgraph Spanning subgraph

15 CONNECTIVITY Connected graph Non connected graph with two connected components

16 TREES AND FORESTS Tree Forest

17 SPANNING TREES AND FORESTS A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree

18 GRAPH ADT

19 EXERCISE ON ADT ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

20 EDGE LIST STRUCTURE An edge list can be stored in a sequence, a vector, a list or a dictionary such as a hash table ORD PVD MIA DFW LGA 849 802 1387 1099 1120 142 (ORD, PVD) (ORD, DFW) (LGA, PVD) (LGA, MIA) (DFW, LGA) 849 802 142 1099 1387 (DFW, MIA) 1120 Edge List ORD LGA PVD DFW MIA Vertex Sequence

21 EXERCISE EDGE LIST STRUCTURE Construct the edge list for the following graph u x y v z a

22 ASYMPTOTIC PERFORMANCE EDGE LIST STRUCTURE Edge List Space ? ? ? ? ? (ORD, PVD) (ORD, DFW) (LGA, PVD) (LGA, MIA) (DFW, LGA) 849 802 142 1099 1387 (DFW, MIA) 1120 Edge List ORD LGA PVD DFW MIA Vertex Sequence 2 3 2 3 2 False WeightDirectedDegree

23 ASYMPTOTIC PERFORMANCE EDGE LIST STRUCTURE Edge List Space (ORD, PVD) (ORD, DFW) (LGA, PVD) (LGA, MIA) (DFW, LGA) 849 802 142 1099 1387 (DFW, MIA) 1120 Edge List ORD LGA PVD DFW MIA Vertex Sequence 2 3 2 3 2 False WeightDirectedDegree

24 EDGE LIST STRUCTURE Vertex object element reference to position in vertex sequence Edge object element origin vertex object destination vertex object reference to position in edge sequence Vertex sequence sequence of vertex objects Edge sequence sequence of edge objects v u w ac b a z d uv w z bcd

25 ADJACENCY LIST STRUCTURE Adjacency Lists associate edges with their end vertices Each vertex stores a list of incident edges ORD PVD MIA DFW LGA 849 802 1387 1099 1120 142 ORD LGA PVD DFW MIA (ORD, PVD) Adjacency List (ORD, DFW) (LGA, PVD) (LGA, MIA) (PVD, ORD) (PVD, LGA) (LGA, DFW) (DFW, ORD) (DFW, LGA)(DFW, MIA) (MIA, LGA) (MIA, DFW)

26 EXERCISE ADJACENCY LIST STRUCTURE Construct the adjacency list for the following graph u x y v z a

27 ASYMPTOTIC PERFORMANCE ADJACENCY LIST STRUCTURE Adjacency List Space ? ? ? ? ? ORD LGA PVD DFW MIA (ORD, PVD) Adjacency List (ORD, DFW) (LGA, PVD) (LGA, MIA) (PVD, ORD) (PVD, LGA) (LGA, DFW) (DFW, ORD) (DFW, LGA)(DFW, MIA) (MIA, LGA) (MIA, DFW)

28 ASYMPTOTIC PERFORMANCE ADJACENCY LIST STRUCTURE Adjacency List Space ORD LGA PVD DFW MIA (ORD, PVD) Adjacency List (ORD, DFW) (LGA, PVD) (LGA, MIA) (PVD, ORD) (PVD, LGA) (LGA, DFW) (DFW, ORD) (DFW, LGA)(DFW, MIA) (MIA, LGA) (MIA, DFW)

29 ADJACENCY LIST STRUCTURE Store vertex sequence and edge sequence Each vertex stores a sequence of incident edges Sequence of references to edge objects of incident edges Augmented edge objects References to associated positions in incidence sequences of end vertices u v w ab a uv w b

30 ADJACENCY MATRIX STRUCTURE 01234 000110 100111 211000 311001 401010 Adjacency matrices store edges in a table, indexed by the vertex 0 2 4 3 1

31 EXERCISE ADJACENCY MATRIX STRUCTURE Construct the adjacency matrix for the following graph u x y v z a

32 ADJACENCY MATRIX STRUCTURE IN A WEIGHTED GRAPH 0 ORD 1 LGA 2 PVD 3 DFW 4 MIA 0 ORD 008498020 1 LGA 0014213871099 2 PVD 849142000 3 DFW 802138001120 4 MIA 01099011200 Store edge object/property in table, or include a pointer to it inside of the table 0:ORD 2:PVD 4:MIA 3:DFW 1:LGA 849 802 1387 1099 1120 142

33 EXERCISE ADJACENCY MATRIX STRUCTURE: WEIGHTED DIGRAPH 0 ORD 1 LGA 2 PVD 3 DFW 4 MIA 0 ORD 1 LGA 2 PVD 3 DFW 4 MIA 0:ORD 2:PVD 4:MIA 3:DFW 1:LGA 849 802 1387 1099 1120 142

34 EXERCISE ADJACENCY MATRIX STRUCTURE: WEIGHTED DIGRAPH 0 ORD 1 LGA 2 PVD 3 DFW 4 MIA 0 ORD 0084900 1 LGA 00013871099 2 PVD 0142000 3 DFW 8020000 4 MIA 00011200 0:ORD 2:PVD 4:MIA 3:DFW 1:LGA 849 802 1387 1099 1120 142

35 ASYMPTOTIC PERFORMANCE OF ADJACENCY MATRIX STRUCTURE Adjacency Matrix Space ? ? ? ? ? 01234 000110 100111 211000 311001 401010

36 ASYMPTOTIC PERFORMANCE OF ADJACENCY MATRIX STRUCTURE Adjacency Matrix Space 01234 000110 100111 211000 311001 401010

37 ADJACENCY MATRIX STRUCTURE Augmented vertex objects Integer key (index) associated with vertex 2D-array adjacency array Reference to edge object for adjacent vertices Null for non nonadjacent vertices The “old fashioned” version just has 0 for no edge and 1 for edge u v w ab 012 0  1  2  a uv w 01 2 b

38 ASYMPTOTIC PERFORMANCE Edge List Adjacency List Adjacency Matrix Space

39 DEPTH-FIRST SEARCH DB A C E

40

41 EXAMPLE DB A C E D B A C E D B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge F F F G G G

42 EXAMPLE DB A C E DB A C E DB A C E D B A C E F F F F G G G G

43 DB A C E F G DB A C E F G DB A C E F G

44 DFS AND MAZE TRAVERSAL The DFS algorithm is similar to a classic strategy for exploring a maze We mark each intersection, corner and dead end (vertex) visited We mark each corridor (edge) traversed We keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack)

45 DFS ALGORITHM

46 EXERCISE DFS ALGORITHM Perform DFS of the following graph, start from vertex A Assume adjacent edges are processed in alphabetical order Number vertices in the order they are visited Label edges as discovery or back edges CB A E D F

47 PROPERTIES OF DFS DB A C E F G v1v1 v2v2

48 ANALYSIS OF DFS DB A C E F G

49

50 APPLICATION PATH FINDING

51 APPLICATION CYCLE FINDING

52 BREADTH-FIRST SEARCH CB A E D L0L0 L1L1 F L2L2

53

54 EXAMPLE C B A E D discovery edge cross edge A visited vertex A unexplored vertex unexplored edge L0L0 L1L1 F CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F

55 EXAMPLE CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 discovery edge cross edge visited vertex A A unexplored vertex unexplored edge

56 EXAMPLE CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 discovery edge cross edge visited vertex A A unexplored vertex unexplored edge

57 BFS ALGORITHM

58 EXERCISE BFS ALGORITHM Perform BFS of the following graph, start from vertex A Assume adjacent edges are processed in alphabetical order Number vertices in the order they are visited and note the level they are in Label edges as discovery or cross edges CB A E D F

59 PROPERTIES CB A E D L0L0 L1L1 F L2L2 CB A E D F

60 ANALYSIS

61 ANALYSIS OF BFS

62 APPLICATIONS

63 DFS VS. BFS CB A E D L0L0 L1L1 F L2L2 CB A E D F DFSBFS ApplicationsDFSBFS Spanning forest, connected components, paths, cycles  Shortest paths  Biconnected components 

64 DFS VS. BFS CB A E D L0L0 L1L1 F L2L2 CB A E D F DFSBFS


Download ppt "CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA."

Similar presentations


Ads by Google