1 / 47

Data Structures and Algorithms

Data Structures and Algorithms. Graphs. 1. Graphs. Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms: Graph Traversal Shortest Paths Minimum Cost Spanning Trees. 2. A. F. B. G. E. C. D. 1. Basic Definitions.

matthias
Download Presentation

Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures and Algorithms Graphs 1

  2. Graphs • Basic Definitions • Paths and Cycles • Connectivity • Other Properties • Representation • Examples of Graph Algorithms: • Graph Traversal • Shortest Paths • Minimum Cost Spanning Trees 2

  3. A F B G E C D 1. Basic Definitions A graph G (V,E) can be defined as a pair (V,E), where V is a set of 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”). 3

  4. A F B G E C D Basic Definitions Adjacency: If vertices u,v have an edge e = (u,v) | u, v  V then u 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 3 2 2 1 5 4 2 1 4

  5. A F B G E C D Basic Definitions 3 Directed Graph: is a graph in which adjacency is not symmetric, i.e., (u,v)  (v,u) Directed Weighted Graph: A directed graph with a weight for each edge. Also called a network. 2 2 1 5 4 2 1 5

  6. A F A F B G E B G E C D C D 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. 6

  7. A F A F B G E B G E C D C D Paths & Cycles Hamiltonian Cycle: A cycle that includes all other vertices only once, e.g. {D,B,C,G,A,F,E,D} Euler Circuit: A cycle that includes every edge once. Directed Acyclic Graph (DAG): A directed graph with no path that starts and ends at the same vertex 7

  8. A F A F B G E B G E C D C D 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 8

  9. Connected Components Demo • What 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. 9

  10. A F A F B G E B E C D C D 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 10

  11. A F A F B G E B E C D C D 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 11

  12. A F B G E C D Connectivity Degree 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 12

  13. A F B G E C D 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). 13

  14. A D B C Connectivity Complete Graph: There is an edge between every vertex and every other 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) 14

  15. Connectivity Dense Graph: Number of edges is close to Emax = V(V-1)/2. So, E = (V2) Sparse Graph: Number of edges is close to Emin = (V-1). So, E = O(V) 15

  16. A D A D B C B C 4. Other Properties Planar Graph: A graph that can be drawn in the plain without edges crossing Non-Planar 16

  17. Other Properties Graph Coloring: To assign color (or any distinctive mark) to vertices such that no two adjacent vertices have the same color. The minimum number of colors needed is called the Chromatic Order of the graph (G). For a complete graph, (G) = V. 2 1 4 3 17

  18. Other Properties Bipartite Graphs A bipartite graphis a graph whose vertices can be partitioned into two subsets X and Y such that each edge has one end in X and one in Y. 18

  19. A D B C 5. Representation Adjacency 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) • For weighted graphs, a(i,j) = wij 19

  20. A D B C Representation Adjacency 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. A B C B A C C A B D D C 20

  21. 6. Examples of Graph Algorithms • Examples of Graph Algorithms: • Graph Traversal • Shortest Paths • Minimum Cost Spanning Trees 21

  22. 6.2 Shortest Paths(Dijkstra’s Algorithm) • Dijkstra's algorithm creates labels associated with vertices. These labels represent the distance (cost) from the source vertex to that particular vertex.  Within the graph, there exists two kinds of labels: temporary and permanent. 22

  23. 6.2 Shortest Paths(Dijkstra’s Algorithm) • The temporary labels (in circles) are given to vertices that have not been reached.  The value given to these temporary labels can vary.  • Permanent labels (in squares) are given to vertices that have been reached and their distance (cost) to the source vertex is known.  The value given to these labels is the distance (cost) of that vertex to the source vertex.  23

  24. Shortest path (Example) 24

  25. Shortest path (Example) 25

  26. Shortest path (Example) 26

  27. Shortest path (Example) 27

  28. Shortest path (Example 2) find the shortest path from A to all other nodes using Dijkstra’s algorithm. 28

  29. Shortest path (Example 2) . Source destination length Path A D 1 (a,d) A E 2 (a,d,e) A C 3 (a,d,e,c) A B 2 (a,b) A F 4 (a,d,e,f) 29

  30. 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  E • S is a tree, i.e., S is connected and has no cycles 30

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

  32. 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 A G D B C Spanning Tree 32

  33. One Graph, Several Spanning Trees 33

  34. (b) Minimum Cost Spanning Tree(MST) Consider houses A..F connected by muddy roads with the distances indicated. We want to pave some roads such that: • We can reach a house from any other house via paved roads. • The cost of paving is minimum. This problem is an example of finding a Minimum Spanning Tree (MST) 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 34

  35. 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 cost • For the shown graph, the minimum cost is 22 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 35

  36. Kruskal’s Algorithm for MST • 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). hence the algorithm • Builds 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. 36

  37. Abstract Algorithm -Form Set E of edges in increasing order of costs. - Set MST T = empty -Repeat • Select 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. 37

  38. Example 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 38

  39. Example 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 39

  40. Example 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 40

  41. Example 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 41

  42. Example 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 42

  43. Example 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 43

  44. Example 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 44

  45. Example 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 45

  46. Example 4 G 4 E A 9 2 7 3 3 C B F 6 4 5 D 46

  47. Prime Algorithm for MST The algorithm add one vertex at a time. • It starts at any vertex in a graph (vertex A, for example), and finds the least cost vertex (vertex B, for example) connected to the start vertex. • Now, from either ‘A’ or ‘B’, it will find the next least costly connection, without creating a cycle, and you continue in this manner till all vertices are included. 47

More Related