Download presentation
Presentation is loading. Please wait.
Published byPaul Crawford Modified over 8 years ago
1
1 Data Structures and Algorithms Graphs
2
2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms: Graph Traversal Shortest Paths Minimum Cost Spanning Trees
3
3 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”). A F GBE DC
4
4 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 A F GBE DC 2 3 1 5 1 2 2 4
5
5 Basic Definitions 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. A F GBE DC 2 3 1 5 1 2 2 4
6
6 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. A F GBE DC AF GB E DC
7
7 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 AF GB E DC AF GB E DC
8
8 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 A F GBE DC A F GBE DC
9
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.
10
10 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 A F BE DC A F GBE DC
11
11 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 A F BE DC A F GBE DC
12
12 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 A F GBE DC
13
13 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). AF GB E DC
14
14 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) A D CB
15
15 Connectivity Dense Graph: Number of edges is close to E max = V(V-1)/2. So, E = (V 2 ) Sparse Graph: Number of edges is close to E min = (V-1). So, E = O(V)
16
16 4. Other Properties Planar Graph: A graph that can be drawn in the plain without edges crossing A D CB A D CB Non-Planar
17
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. 1 2 3 4
18
18 Other Properties Bipartite Graphs A bipartite graph is 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.
19
19 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) = w ij A D CB ABCD A0110 B1010 C1101 D0010
20
20 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 D CB C B D B A C AC ABD C
21
21 6. Examples of Graph Algorithms Examples of Graph Algorithms: Graph Traversal Shortest Paths Minimum Cost Spanning Trees
22
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.
23
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.
24
24 Shortest path (Example)
25
25 Shortest path (Example)
26
26 Shortest path (Example)
27
27 Shortest path (Example)
28
28 Shortest path (Example 2) find the shortest path from A to all other nodes using Dijkstra’s algorithm.
29
29 Shortest path (Example 2). Source destination lengthPath A D1(a,d) AE2(a,d,e) AC3(a,d,e,c) AB2(a,b) AF4(a,d,e,f)
30
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
31
31 Spanning Tree S(V,T): V = {A,B,C,D,E,F,G} T = {AB,AF,CD,DE,EF,FG} FE GA D CB
32
32 Spanning Tree 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)) FE GA D CB
33
33 One Graph, Several Spanning Trees
34
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) E G A B C F D 4 4 9 2 3 3 7 64 5
35
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 E G A B C F D 4 4 9 2 3 3 7 64 5
36
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.
37
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.
38
38 Example E G A B C F D 4 4 9 2 3 3 7 64 5 Edge uvw accept 1EF2 2AC3 3CE3 4AE4 5CD4 6EG4 7DF5 8BD6
39
39 Example E G A B C F D 4 4 9 2 3 3 7 64 5 Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 5CD4 6EG4 7DF5 8BD6
40
40 Example E G A B C F D 4 4 9 2 3 3 7 64 5 Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 5CD4 6EG4 7DF5 8BD6
41
41 Example E G A B C F D 4 4 9 2 3 3 7 64 5 Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 5CD4 6EG4 7DF5 8BD6
42
42 Example E G A B C F D 4 4 9 2 3 3 7 64 5 Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 NO 5CD4 6EG4 7DF5 8BD6
43
43 Example E G A B C F D 4 4 9 2 3 3 7 64 5 Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 NO 5CD4yes 6EG4 7DF5 8BD6
44
44 Example E G A B C F D 4 4 9 2 3 3 7 64 5 Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 NO 5CD4yes 6EG4 7DF5 8BD6
45
45 Example E G A B C F D 4 4 9 2 3 3 7 64 5 Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 NO 5CD4yes 6EG4 7DF5 No 8BD6
46
46 Example E G A B C F D 4 4 9 2 3 3 7 64 5 Edge uvw accept 1EF2yes 2AC3 3CE3 4AE4 NO 5CD4yes 6EG4 7DF5 NO 8BD6yes
47
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.