Introduction to Graphs

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Shortest Paths Definitions Single Source Algorithms
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Upon completion you will be able to:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs Chapter 20.
CS202 - Fundamental Structures of Computer Science II
Graphs Representation, BFS, DFS
Shortest Paths and Minimum Spanning Trees
Graphs CS Data Structures Mehmet H Gunes
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
C.Eng 213 Data Structures Graphs Fall Section 3.
Algorithms and Data Structures Lecture XIII
Ellen Walker CPSC 201 Data Structures Hiram College
Common final examinations
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graphs.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
Graph Algorithm.
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Graphs Chapter 11 Objectives Upon completion you will be able to:
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Algorithms and Data Structures Lecture XIII
Chapter 11 Graphs.
Shortest Path Algorithms
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
ITEC 2620M Introduction to Data Structures
Fundamental Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Algorithms Searching in a Graph.
Text Book: Introduction to algorithms By C L R S
Fundamental Data Structures and Algorithms
Chapter 16 1 – Graphs Graph Categories Strong Components
CSE 417: Algorithms and Computational Complexity
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
GRAPH – Definitions A graph G = (V, E) consists of
GRAPHS.
Topological Sorting Minimum Spanning Trees Shortest Path
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Introduction to Graphs In many cases we are faced with a problem that is defined in terms of a number of objects or entities that have some relationships among them. We then try to answer interesting questions. Campus map Travelling salesperson Circuit layout Project scheduling Oil flow Flight scheduling Graphs are the basic mathematical formulation we use too tackle such problems. Basic definitions and properties. Computer representation. Some applications. Graphs, part I 1

What is a Graph? A Graph G consists of a set V of vertices or nodes and a set E of edges that connect the vertices. We write G=(V,E). v1 v5 v4 v2 v3 e1 e4 e3 e2 G=(V,E) V={v1,v2,v3,v4,v5} E={e1,e2,e3,e4} e1=(v1,v2) e2=(v2,v3) e3=(v2,v4) e4=(v3,v5) Graphs, part I 2

Graphs --- Examples v1 G=(V,E) V={v1,v2,v3,v4,v5} v2 E={e1,e2} E={e1,e2,e3,e4,e5,e6} e1=(v1,v2) e2=(v1,v3) e3=(v2,v3) e4=(v2,v4) e5=(v3,v4) e6=(v1,v4) Graphs, part I 3

(predecessor, successor) Directed Graphs In some cases we want the edges to have directions associated with them; we call such a graph a directed graph or a digraph. G=(V,E) V={v1,v2,v3,v4} E={e1,e2,e3,e4} e1=(v2,v1) e2=(v1,v3) e3=(v1,v4) e4=(v4,v3) e5=(v3,v4) v1 v4 v2 v3 e1 e4 e2 e3 e5 ordered pair (predecessor, successor) Graphs, part I 4

Weighted Graphs In some cases, we want to associate a weight with each edge in the graph. Such a graph is known as a weighted graph. Graphs with no weights are called unweighted graphs (or simply graphs). Directed graphs can also be weighted (directed weighted graphs). G=(V,E) V={v1,v2,v3,v4} E={e1,e2,e3,e4,e5} ………………….. v1 v4 v2 v3 40 63 15 50 75 55 Graphs, part I 5

More Graph Terminology A vertex vj is said to be adjacent to a different vertex vi if an edge connects vi to vj, i.e., if there exists and edge e Î E such that e=(vi,vj). A path is a sequence of vertices in which each vertex is adjacent to the next one. That is, a path p = v1, v2, … , vn (n > 1) such that each vertex vi+1 is adjacent to vi, 1 £ i < n. The length of a path is the number of edges in it. A cycle is a path of length greater than one that begins and ends at the same vertex. In other words, a cycle is a path p = v1, v2, … , vn, such that v1 = vn. A graph with no cycles is called an acyclic graph. A directed acyclic graph is called a DAG. Graphs, part I 6

More Graph Terminology (Cont’d) A simple cycle is a cycle formed from three or more distinct vertices in which no vertex is visited more than once along the simple cycle’s path (except for the starting and ending vertex). That is, if p = v1, v2, … , vn (n > 3) is a path, then p is a simple cycle if v1 = vn , and vi ¹ vj for different i and j in the range 1 £ i,j < n. Two different vertices are connected if there is a path between them. A subset of vertices S is said to be a connected component of G if there is a path from each vertex vi to any other distinct vertex vj of S. If S is the largest such subset, then it is called a maximal connected component. The degree of a vertex is the number of edges connected to it. Graphs, part I 7

Graph Terminology --- Example 1 4 5 6 3 2 8 7 10 9 11 12 13 14 15 Graphs, part I 8

Representation of Graphs The adjacency matrix for a graph G=(V,E) with n (or |V|) vertices numbered 0, 1, …, n-1 is an n x n array M such that M[i][j] is 1 if and only if there is an edge from vertex i to vertex j. The adjacency list for a graph G=(V,E) with n vertices numbered 0, 1, …, n-1 consists of n linked lists. The ith linked list has a node for vertex j if and only if the graph contains and edge from vertex i to vertex j. Which one is better? Graphs, part I 9

Adjacency Matrix --- Example 1 4 5 6 3 2 8 7 1 2 3 4 5 6 7 8 Graphs, part I 10

Adjacency Matrix --- Example 2 3 1 6 8 9 ¥ 8 6 9 1 2 3 The matrix is symmetric for undirected graphs. Graphs, part I 11

Adjacency List --- Example 1 4 5 6 3 2 8 7 1 2 3 4 7 5 6 8 Graphs, part I 12

Adjacency List --- Example 2 3 1 6 8 9 1 2 3 8 6 9 Graphs, part I 13

Which is Better? Operation 1: Is there an edge from vertex i to vertex j? Operation 2: Find all vertices adjacent to vertex i. Time: Operation 1 Operation 2 M[i][j] O(1) Search List O(d) Traverse List Traverse row O(n) Matrix List Determine which operation is most frequent. Graphs, part I 14

Which is Better? Space: n2 x size of integer; i.e., O(n2). Matrix: List: n x size of pointer + O(|E|) x (size of integer + size of pointer) O(n+|E|) = O(|V| + |E|) How big is this? Consider space given graph properties. Graphs, part I 15

Graph Traversals Traversal: visit each vertex in the graph once and only once. 1 2 3 4 5 6 7 8 9 1 4 5 6 3 2 7 9 8 Graphs, part I 16

Graph Traversals --- Basic Algorithm while ($ un-VISITED vertex) { Initialize a DS with un-VISITED vertex vI ;mark as VISITED while (DS is not empty) { remove a vertex vj from DS visit vj add un-VISITED vertices adjacent to vj to DS; mark each as VISITED } DS is a queue --- breadth-first traversal (search) DS is a stack --- depth-first traversal (search) Graphs, part I

Breadth-First Traversal 1 3 6 2 4 5 1 4 5 6 3 2 Spanning tree. Graphs, part I 18

Depth-First Traversal 1 4 5 6 3 2 1 3 6 5 2 4 Spanning tree. Graphs, part I 19

Recursive Depth-First Traversal DFS(vi) visit vj; mark vi as VISITED for each un-VISITED vertex vj adjacent to vi { DFS (vj) } 1 4 5 6 3 2 Graphs, part I

Graph Traversals --- Example I E M D L H F C B J G K BF: A B C F G D E H I J K L M A I E M D L H F C B J G K DF: A G E D F C B H I J M L K non-Recursive A I E M D L H F C B J G K DF: A B C F D E G H I J K L M Recursive Graphs, part I 21

Complexity of Graph Traversals Each vertex must be visited exactly once. At a vertex, we must determine all other vertices connected to the vertex. Adjacency matrix: O(|V|2). Adjacency list: O(|V| + |E|). Each edge is examined once (directed) or twice (undirected). Typically, lists are better than matrices. The complexity of the traversal is linear in the number of edges. Graphs, part I

Elementary Graph Operations Graph traversals provide the basis for many elementary graph operations: Spanning trees on graphs Graph cycles Connected components of a graph Graphs, part I

Minimum-Cost Spanning Trees A minimum-cost spanning tree of a connected weighted graph is a collection of edges connecting all vertices such that the sum of the weights of the edges is the smallest possible. 6 6 b b 7 7 a a 2 9 c 2 9 c 3 3 e 4 e 4 h h 4 4 8 8 d d g g 5 5 f f 2 2 Prim’s algorithm: always pick the edge with the smallest weight to any node. It is a greedy algorithm.

MCST --- Example b b a a c c e e h h d d g g f f b b a a c c e e h h d 6 6 b b 7 7 a a 2 9 c 2 9 c 3 3 e 4 e 4 h h 4 4 8 8 d d g g 5 5 f f 2 2 6 6 b b 7 7 a a 2 9 c 2 9 c 3 3 e 4 e 4 h h 4 4 8 8 d d g g 5 5 f f 2 2

MCST --- Example (cont’d) 6 6 b b 7 7 a a 2 9 c 2 9 c 3 3 e 4 e 4 h h 4 4 8 8 d d g g 5 5 f f 2 2 6 6 b b 7 7 a a 2 9 c 2 9 c 3 3 e 4 e 4 h h 4 4 8 8 d d g g 5 5 f f 2 2

Prim’s Algorithm for MCST S = {0} /* Set of edges in current MCST */ for i = 0 to n-1 D[i] = M[0][i] /* Weight of smallest edge from i to S */ V[i] = 0 /* Vertex in S that i connects to */ for i = 1 to n-1 find the smallest D[v] such that v  S S = S  {v} MCST = MCST + ( v, V[v] ) for all vertices u  S if( D[u] > M[u][v] ) then D[u] = M[u][v] V[u] = v Graphs, part I

Prim’s Algorithm: Heap Implementation The following implements Prim’s algorithm with a heap: S = {0} /* Set of edges in current MCST */ ……. loop 1 for i = 0 to n D[i] = M[0][i] /* Weight of smallest edge from i to S */ V[i] = 0 /* Vertex in S that i connects to */ ……. loop 2 for i = 1 to n … Extract_Min() find the smallest D[v] such that v Ï S S = S È {v} MCST = MCST + ( v, V[v] ) ……. loop 3 for all vertices u Ï S if( D[u] > M[u][v] ) then … Decrease_Key() D[u] = M[u][v] V[u] = v Graphs, part I

Prim’s Algorithm: Heap Implementation Consider that you use a heap to hold the keys D[0…n-1]. In this heap children are larger than the root (heap property): To find the minimum in loop 2 you need O(log V) time with procedure Extract_Min(heap) To update the values of D[0…n-1] in loop 3 you need a procedure Decrease_Key(heap, node, new_key) that takes a pointer to a node in the heap and decreases its key. This can be done in O(logV) time ….. HOW? Graphs, part I

Prim’s Algorithm: Heap Implementation Loop 1: Executed V times. Total time: O(V) Loop 2: Executed V times. To extract the minimum we need O(log V) time at each iteration. Total time: O(VlogV) Loop 3: It is executed a total of 2E times and it takes O(log V) time at each iteration to decrease the key. Therefore, total time is O(E logV) Heap Implementation: O(V+VlogV +ElogV) = O(ElogV) Graphs, part I

Single Source Shortest Paths Given a weighted connected graph G=(V,E), and given a pair of vertices vs (source) and vd (destination) Î V what is the shortest path from vs to vd? That is, what is the path that has the smallest sum of edge weights? 1 2 4 6 3 5 10 7 8 1 2 4 6 3 5 10 7 8 Source vertex vs is vertex 1 Bold line is shortest path from 1(vs) to 5 (vd)

Single Source Shortest Paths A B E F H 15 A B E G H 14 A C E F H 16 A C E G H 15 A D E F H 26 A D E G H 25 B 1 2 F 5 7 1 3 A C E H 5 6 6 8 G D SP from A to H = SP from A to E + SP from E to H. SP from A to H = SP from A to B + SP from B to H. SP from A to H = SP from A to C + SP from C to H. In general: SP from A to H = SP from A to vi + SP from vi to H; " vi.

Single Source Shortest Paths Let D[vs, v] be the distance from the source vertex vs to some vertex v and let M[u, v] be the weight of edge uv for vertices u and v. At the end of any correct shortest path algorithm you should have: D[vs, v] D[vs, u] + W(v, u) (why?) Use Dijkstra’s (greedy) algorithm if graph has only positive weights Use Bellman-Ford’s algorithm if graph has positive and negative weights (but not negative cycles…) Both algorithms can run on directed or undirected graphs Graphs, part I

Dijkstra’s Algorithm Cost of SP from A to vi through S S = {A} B 1 2 F 5 A B C D E F G H 7 1 3 A C E H -- 2 1 6 ? ? ? ? 5 6 6 8 G S = {A} D B 1 2 F 5 A 2 B C 6 D 4 E ? F ? G ? H 7 1 3 A C E H -- 1 5 6 6 8 G S = {A,C} D

Dijkstra’s Algorithm (Cont’d) Cost of SP from A to vi through S B 1 2 F 5 A B C D E F G H 7 1 3 A C E H -- 2 1 6 3 ? ? ? 5 6 6 8 G S = {A,C,B} D B 1 2 F 5 A B C D E F G H 7 1 3 A C E H -- 2 1 6 3 10 8 ? 5 6 6 8 G S = {A,C,B,E} D

Dijkstra’s Algorithm (Cont’d) Cost of SP from A to vi through S B 1 2 F 5 A B C D E F G H 7 1 3 A C E H -- 2 1 6 3 10 8 ? 5 6 6 8 G S = {A,C,B,E,D} D B 1 2 F 5 A B C D E F G H 7 1 3 A C E H -- 2 1 6 3 10 8 14 5 6 6 8 G S = {A,C,B,E,D,G} D

Dijkstra’s Algorithm (Cont’d) Cost of SP from A to vi through S B 1 2 F 5 A B C D E F G H 7 1 3 A C E H -- 2 1 6 3 10 8 14 5 6 6 8 G S = {A,C,B,E,D,G,F} D B 1 2 F 5 A B C D E F G H 7 1 3 A C E H -- 2 1 6 3 10 8 14 5 6 6 8 G S = {A,C,B,E,D,G,F,H} D

Dijkstra’s Algorithm for Shortest Paths S = {0} /* Current MST */ for i = 0 to n D[i] = M[0][i] /* Shortest path length from 0 to i */ for i = 1 to n-1 find the smallest D[v] such that v  S S = S  {v} for all vertices u  S if (D[u] > D[v] + M[v][u]) then D[u] = D[v] + M[v][u]

Dijkstra’s Algorithm --- Example 1 2 3 4 5 6 -- 3 ? ? ? 5 1 2 3 4 5 6 -- 3 10 ? ? 5 1 2 4 6 3 5 10 7 8 1 2 3 4 5 6 -- 3 10 7 12 5 1 2 3 4 5 6 -- 3 10 7 12 5 1 2 3 4 5 6 -- 3 10 7 11 5 1 2 3 4 5 6 -- 3 10 7 11 5

Bellman-Ford’s Algorithm If the graph also contains negative weights then Dijkstra’s will not work. Use Bellman-Ford’s algorithm The graph should never contain negatives cycles! (because the idea of a shortest path cannot be defined) Bellman-Ford’s algorithm returns FALSE in this case Recall, let D[vs, v] be the distance from the source vertex vs to some vertex v and let M[u, v] be the weight of edge uv for vertices u and v. Graphs, part I

Bellman-Ford’s Algorithm --- Algorithm for i = 0 to n D[i] = INFINITE /* Shortest path length from 0 to i */ end for for i = 1 to n-1 for each edge u  v in the graph do if (D[v] > D[u] + M[u][v]) then D[v] = D[u] + M[v][u] return FALSE /* negative cycle exists */ Graphs, part I

Bellman-Ford’s Algorithm --- Example Edges are directed with positive and negative weights Each vertex holds D[v] value Bold edges the ones if-statement executes in 1st loop 6 8 -2 -3 7 9 5 2 -4 vs Initial graph after i=1 INF Graphs, part I

Bellman-Ford’s Algorithm --- Example 5 6 4 5 vs 6 -2 vs 2 4 6 -2 -3 8 7 -3 8 -4 7 7 2 -4 7 2 7 2 9 7 2 9 after i=2 after i=3 5 5 vs 2 4 vs 2 4 6 6 -2 -2 -3 -3 8 8 7 7 -4 -4 7 7 2 2 7 -2 7 -2 9 9 after i=4 Graphs, part I after i=5… (final)

Bellman-Ford’s Algorithm In the 1st nested loop, the algorithm repeats n (= V) times. Each time it visits every vertex E It takes O(VE + E) = O(VE) time (VE time to execute the 1st nested loop and E time to execute the 2nd loop ) If the graph has negative weighted cycles then the idea of a shortest path cannot be defined: One could possibly traverse the cycle infinitely and keep reducing the length of the shortest path Bellman-Ford returns FALSE in this case Graphs, part I