CMSC 341 Graphs.

Slides:



Advertisements
Similar presentations
CMSC 341 Graphs 3. Adjacency Table Implementation Uses table of size |V|  |V| where each entry (i,j) is boolean –TRUE if there is an edge from vertex.
Advertisements

§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
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.
CMSC 380 Graph Traversals and Search. 2 Graph Traversals Graphs can be traversed breadth-first, depth- first, or by path length We need to specifically.
CMSC 341 Graphs. 8/3/2007 UMBC CMSC 341 Graphs 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
CMSC 341 Graphs. 5/5/20062 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge.
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.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
Graphs Spring 2016CS202 - Fundamental Structures of Computer Science II1 Initially prepared by Dr. Ilyas Cicekli; improved by various Bilkent CS202 instructors.
Introduction to Algorithms
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CS202 - Fundamental Structures of Computer Science II
Chapter 22 Elementary Graph Algorithms
CS212: Data Structures and Algorithms
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
CMSC 341 Graphs.
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
Cse 373 May 8th – Dijkstras.
CS202 - Fundamental Structures of Computer Science II
Introduction to Graphs
Depth-First Search.
Common final examinations
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graphs.
CS202 - Fundamental Structures of Computer Science II
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs Graph transversals.
Lecture 10 Algorithm Analysis
Graph & BFS.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Basic Graph Algorithms
CMSC 341 Graphs 3.
CMSC 341 Graphs 3.
Shortest Path Algorithms
Chapter 11 Graphs.
CMSC 341 Lecture 20.
ITEC 2620M Introduction to Data Structures
CSE 373: Data Structures and Algorithms
CE 221 Data Structures and Algorithms
Chapter 16 1 – Graphs Graph Categories Strong Components
Data Structures and Algorithm Analysis Graph Algorithms
Graphs G = (V, E) V are the vertices; E are the edges.
CMSC 341 Graphs 2.
Graph Algorithms DS.GR.1 Chapter 9 Overview Representation
CE 221 Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
Algorithm Course Dr. Aref Rashad
GRAPH – Definitions A graph G = (V, E) consists of
CMSC 341 Graphs.
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:

CMSC 341 Graphs

Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge is a pair (v,w) where v, w  V. V and E are sets, so each vertex v  V is unique, and each edge e  E is unique. Edges are sometimes called arcs or lines. Vertices are sometimes called nodes or points. Show examples of directed graph undirected graph Applications: cities/streets cities/airline routes computers/network connections 5/5/2006

Graph Applications Graphs can be used to model a wide range of applications including Intersections and streets within a city Roads/trains/airline routes connecting cities/countries Computer networks Electronic circuits 5/5/2006

Basic Graph Definitions (2) A directed graph is a graph in which the edges are ordered pairs. That is, (u,v)  (v,u), u, v  V. Directed graphs are sometimes called digraphs. An undirected graph is a graph in which the edges are unordered pairs. That is, (u,v) = (v,u). A sparse graph is one with “few” edges. That is |E| = O( |V| ) A dense graph is one with “many” edges. That is |E| = O( |V|2 ) 5/5/2006

Undirected Graph 2 1 3 4 5 All edges are two-way. Edges are unordered pairs. V = { 1, 2 ,3, 4, 5} E = { (1,2), (2, 3), (3, 4), (2, 4), (4, 5), (5, 1) } 5/5/2006

Directed Graph 1 5 2 3 4 All edges are “one-way” as indicated by the arrows. Edges are ordered pairs. V = { 1, 2, 3, 4, 5} E = { (1, 2), (2, 4), (3, 2), (4, 3), (4, 5), (5, 4), (5, 1) } 5/5/2006

A Single Graph with Multiple Components 7 6 9 8 2 1 3 4 5 5/5/2006

Basic Graph Definitions (3) Vertex w is adjacent to vertex v if and only if (v, w)  E. For undirected graphs, with edge (v, w), and hence also (w, v), w is adjacent to v and v is adjacent to w. An edge may also have: weight or cost -- an associated value label -- a unique name The degree of a vertex, v, is the number of vertices adjacent to v. Degree is also called valence. Does it matter in an undirected graph? No. Give examples: adjacent to (in undirected: a is adjacent to b but not d) adjacent from (in directed: a is adjacent to b, adjacent from e, but neither adjacent to/from d) degree (in undirected: a has degree 2) indegree (in directed: a has indegree 1 and b has indegree 1) outdegree (in directed: a has outdegree 1, b has outdegree 1 5/5/2006

Paths in Graphs A path in a graph is a sequence of vertices w1, w2, w3, …, wn such that (wi, wi+1)  E for 1  i < n. The length of a path in a graph is the number of edges on the path. The length of the path from a vertex to itself is 0. A simple path is a path such that all vertices are distinct, except that the first and last may be the same. A cycle in a graph is a path w1, w2, w3, …, wn , w  V such that: there are at least two vertices on the path w1 = wn (the path starts and ends on the same vertex) if any part of the path contains the subpath wi, wj, wi, then each of the edges in the subpath is distinct (i. e., no backtracking along the same edge) A simple cycle is one in which the path is simple. A directed graph with no cycles is called a directed acyclic graph, often abbreviated as DAG Example: simple path (in undirected: from a to d there are two simple paths of length 2 (abd and aed) and a path of length 3 (abcd) (in undirected: from a to d there is one simple path (abd) simple cycle(in undirected: abdea, bdcb. The cycle abcdba is not simple) (in directed: abdea bdcb. The cycle vdcbdeab is not simple) 5/5/2006

Connectedness in Graphs An undirected graph is connected if there is a path from every vertex to every other vertex. A directed graph is strongly connected if there is a path from every vertex to every other vertex. A directed graph is weakly connected if there would be a path from every vertex to every other vertex, disregarding the direction of the edges. A complete graph is one in which there is an edge between every pair of vertices. A connected component of a graph is any maximal connected subgraph. Connected components are sometimes simply called components. Disjoint sets can be used to determine connectedness Vi and Vj placed in the same set if they are connected. When all edges Have been examined, each set of Vk’s are connected 5/5/2006

Disjoint Sets and Graphs Disjoint sets can be used to determine connected components of an undirected graph. For each edge, place its two vertices (u and v) in the same set -- i.e. Union( u, v ) When all edges have been examined, the forest of sets will represent the connected components. Two vertices, x, y, are connected if and only if Find( x ) = Find( y ) Slide 6 (graph with multiple components) is easy example 5/5/2006

Undirected Graph/Disjoint Set Example 7 6 9 8 2 1 3 4 5 Sets representing connected components { 1, 2, 3, 4, 5 } { 6 } { 7, 8, 9 } 5/5/2006

DiGraph / Strongly Connected Components 1 2 7 4 3 6 8 Is it strongly connected? -- NO Strongly connected components: g hij bacf d e Weakly connected component: whole graph 5 10 9 5/5/2006

A Graph ADT Has some data elements Has some operations Vertices and Edges Has some operations GetDegree( u ) -- returns the degree of vertex u GetAdjacent( u ) -- returns a list of the vertices adjacent to vertex u IsAdjacentTo ( u, v ) -- returns TRUE if vertex v is adjacent to vertex u, FALSE otherwise Has some associated algorithms to be discussed. 5/5/2006

Adjacency Matrix Implementation Uses array of size |V|  |V| where each entry (i ,j) is boolean TRUE if there is an edge from vertex i to vertex j FALSE otherwise store weights when edges are weighted Very simple, but large space requirement = O(|V|2) Appropriate if the graph is dense. Otherwise, most of the entries in the table are FALSE. For example, if a graph is used to represent a street map like Manhattan in which most streets run E/W or N/S, each intersection is attached to only 4 streets and |E| < 4*|V|. If there are 3000 intersections, the table has 9,000,000 entries of which only 12,000 are TRUE. 5/5/2006

Undirected Graph / Adjacency Matrix 2 1 3 4 5 5/5/2006

Directed Graph / Adjacency Matrix 1 5 2 3 4 5/5/2006

Weighted, Directed Graph / Adjacency Matrix 1 8 2 2 5 6 5 7 2 3 4 3 5/5/2006

Adjacency Matrix Performance Storage requirement: O( |V|2 ) Performance: GetDegree ( u ) IsAdjacentTo( u, v ) GetAdjacent( u ) Storage: O(|V|^2) GetDegree(u) O(|V|) -- must look at (|V|-1) elements on the u-th row GetAdjacent(u) O(|V|) -- must look down the u-th row IsAdjacentTo(u,v) O(1) -- random access to element[u,v] 5/9/2006

Adjacency List Implementation If the graph is sparse, then keeping a list of adjacent vertices for each vertex saves space. Adjacency Lists are the commonly used representation. The lists may be stored in a data structure or in the Vertex object itself. Vector of lists: A vector of lists of vertices. The i-th element of the vector is a list, Li, of the vertices adjacent to vi. If the graph is sparse, then the space requirement is O( |E| + |V| ), “linear in the size of the graph” If the graph is dense, then the space requirement is O( |V|2 ) 5/5/2006

Vector of Lists 5 2 3 4 8 1 6 7 1 2 2 4 3 2 4 3 5 5 1 4 5/5/2006

Adjacency List Performance Storage requirement: Performance: GetDegree( u ) IsAdjacentTo( u, v ) GetAdjacent( u ) Storage: O(|V| + |E|) GetDegree(u) -- O(D(u)) -- requires counting list length in each case except when degree is kept in a separate vector which is then O(1) GetAdjacent(u) -- O(D(u)) -- requires construction of a list of each vertex of each vertex on the adjacency list of vertex u IsAdjacentTo(u) - O(D(u)) -- requires searching the adjacency list of vertex u for the presence of vertex v Storage -- O(|V|+|E|) for “Array of Lists”-- entry for each vertex and list of entries for each adjacent vertex 5/9/2006

Graph Traversals Like trees, graphs can be traversed breadth-first or depth-first. Use stack (or recursion) for depth-first traversal Use queue for breadth-first traversal Unlike trees, we need to specifically guard against repeating a path from a cycle. Mark each vertex as “visited” when we encounter it and do not consider visited vertices more than once. 5/5/2006

Breadth-First Traversal Queue<Vertex> q; Vertex u, w; for all v, d[v] =  // mark each vertex unvisited q.enqueue(startvertex); // start with any vertex d[startvertex] = 0; // mark visited while ( !q.isEmpty() ) { u = q.dequeue( ); for (each vertex, w, that is adjacent to u) if (d[w] == ) { // w not marked as visited d[w] = d[u]+1; // mark visited path[w] = u; // where we came from q.enqueue(w); } 5/9/2006

Breadth-First Example v3 v1 v2 v5 Traversal 0: marks all unvisited 1: marks v1 as visited 2. Enqueue and mark v2, v3 3. Dequeue v2, enqueue and mark v4 4. Dequeue v3 -- no op 5. Dequeue v4 -- no op BFS order: 1 2 3 4 Is v1 reachable from v2 ? Yes if v2 does not end up with  mark after BFS from v1 What are minimum number of vertices that must be visited on a path from v1 to vk? D[vk] v4 5/5/2006

Unweighted Shortest Path Problem Unweighted shortest-path problem: Given as input an unweighted graph, G = ( V, E ), and a distinguished starting vertex, s, find the shortest unweighted path from s to every other vertex in G. After running BFS algorithm with s as starting vertex, the length of the shortest path length from s to i is given by d[i]. If d[i] =  , then there is no path from s to i. The path from s to i is given by traversing path[] backwards from i back to s. 5/5/2006

Recursive Depth First Traversal dfs(Graph G) { for (each v  V) dfs(v) } dfs(Vertex v) { if (v is not marked as visited) MarkVisited(v); for(each vertex, w, that is adjacent to v) if ( w is not marked as visited ) dfs(w) 5/9/2006

DFS with explicit stack dfs( Graph G ) { Stack<Vertex> s; Vertex u, w; s.push(startvertex); MarkVisited(startvertex); while ( !s.isEmpty() ) { u = s.Pop(); for (each vertex, w, that is adjacent to u) if (w is not marked as visited) { MarkVisited(w); s.push(w); } Edges traversed form a tree which includes all visited vertices in the graph. This is called a spanning tree. 5/5/2006

DFS Example v3 v1 v2 v5 v4 5/5/2006 Traversal 0: 1: 2. 3. 4. 5. DFS order: 1 2 4 3 v4 5/5/2006

Traversal Performance What is the performance of DF and BF traversal? Each vertex appears in the stack or queue exactly once in the worst case. Therefore, the traversals are at least O( |V| ). However, at each vertex, we must find the adjacent vertices. Therefore, df- and bf-traversal performance depends on the performance of the GetAdjacent operation. 5/5/2006

GetAdjacent Method 1: Look at every vertex (except u), asking “are you adjacent to u?” List<Vertex> L; for (each vertex v, except u) if (IsAdjacentTo(u,v)) L.push_back(v); Assuming O(1) performance for push_back and IsAdjacentTo, then GetAdjacent has O( |V| ) performance and traversal performance is O( |V2| ); 5/5/2006

GetAdjacent (2) Method 2: Look only at the edges which impinge on u. Therefore, at each vertex, the number of vertices to be looked at is D(u), the degree of the vertex This approach is O( D( u ) ). The traversal performance is since GetAdjacent is done O( |V| ) times. However, in a disconnected graph, we must still look at every vertex, so the performance is O( |V| + |E| ). V å O ( |E| ) O ( D ( v )) = i i = 1 5/5/2006

Number of Edges Theorem: The number of edges in an undirected graph G = (V,E ) is O(|V|2) Proof: Suppose G is fully connected. Let p = |V|. Then we have the following situation: vertex connected to 1 2,3,4,5,…, p 2 1,3,4,5,…, p … p 1,2,3,4,…,p-1 There are p(p-1)/2 = O(|V|2) edges. So O(|E|) = O(|V|2). If df an bf traversal is O(|V|^2) under both approaches to the operation getAdjacent, does it mater which approach is used? Yes. Although the two approaches have the same asymptotic performance, the second approach will be faster for the typical graph. 5/5/2006

Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = ( V, E ), and a distinguished starting vertex, s, find the shortest weighted path from s to every other vertex in G. Use Dijkstra’s algorithm keep tentative distance for each vertex giving shortest path length using vertices visited so far Record vertex visited before this vertex (to allow printing of path) at each step choose the vertex with smallest distance among the unvisited vertices (greedy algorithm) 5/5/2006

Dijkstra’s Algorithm The pseudo code for Dijkstra’s algorithm assumes the following structure for a Vertex object struct Vertex { List adjacent; // adjacency list bool known; // specific for Dijkstra int distance; Vertex path; // our predecessor }; 5/5/2006

Dijkstra’s Algorithm Vertex v, w; For all vertices, v v.distance = INFINITY; v.known = FALSE; v.path = NONE; start.distance = 0; while there are unknown vertices v = unknown vertex with smallest distance v.known = TRUE; for each w adjacent to v if (!w.known) if (v.distance + weight(v, w) < w.distance) { decrease( w.distance to v.distance + weight(v, w)) w.path = v; } Mention here about using PQs for Dijkstra If the unknown vertices are kept in a PQ with their distance being the priority, then selecting the “unknown vertex with smallest distance” is PQ.deltemin( ); 5/5/2006

Dijkstra Example 3 v1 v2 v7 1 3 1 2 7 5 3 v4 v3 v6 v8 1 6 4 4 2 v5 v10 5/5/2006

Correctness of Dijkstra’s Algorithm The algorithm is correct because of a property of shortest paths: If Pk = v1, v2, ..., vj, vk, is a shortest path from v1 to vk, then Pj = v1, v2, ..., vj, must be a shortest path from v1 to vj, otherwise Pk would not be as short as possible since Pk extends Pj by just one edge (from vj to vk) Also, Pj must be shorter than Pk (assuming that all edges have positive weights). So the algorithm must have found Pj on an earlier iteration than when it found Pk. i.e. Shortest paths can be found by extending earlier known shortest paths by single edges, which is what the algorithm does. See www.csse.monash.edu/~lloyd/tildeAlgDS/Graph/Directed 5/5/2006

Running Time of Dijkstra’s Algorithm The running time depends on how the vertices are manipulated. The main ‘while’ loop runs O( |V| ) time (once per vertex) Finding the “unknown vertex with smallest distance” (inside the while loop) can be a simple linear scan of the vertices and so is also O( |V| ). With this method the total running time is O (|V|2 ). This is acceptable (and perhaps optimal) if the graph is dense ( |E| = O (|V|2 ) ) since it runs in linear time on the number of edges. If the graph is sparse, ( |E| = O (|V| ) ), we can use a priority queue to select the unknown vertex with smallest distance, using the deleteMin operation (O( lg |V| )). We must also decrease the path lengths of some unknown vertices, which is also O( lg|V| ). The deleteMin operation is performed for every vertex, and the “decrease path length” is performed for every edge, so the running time is O( |E| lg|V| + |V|lg|V|) = O( (|V|+|E|) lg|V|) = O(|E| lg|V|) if all vertices are reachable from the starting vertex See CLR – p 530 Note that building the PQ as a binary heap is O( |V| ) 12/5/2006

Dijkstra and Negative Edges Note in the previous discussion, we made the assumption that all edges have positive weight. If any edge has a negative weight, then Dijkstra’s algorithm fails. Why is this so? Suppose a vertex, u, is marked as “known”. This means that the shortest path from the starting vertex, s, to u has been found. However, it’s possible that there is negatively weighted edge from an unknown vertex, v, back to u. In that case, taking the path from s to v to u is actually shorter than the path from s to u without going through v. Other algorithms exist that handle edges with negative weights for weighted shortest-path problem. 5/5/2006

Directed Acyclic Graphs A directed acyclic graph is a directed graph with no cycles. A strict partial order R on a set S is a binary relation such that for all aS, aRa is false (irreflexive property) for all a,b,c S, if aRb and bRc then aRc is true (transitive property) To represent a partial order with a DAG: represent each member of S as a vertex for each pair of vertices (a,b), insert an edge from a to b if and only if aRb Example: DAG for CS courses 5/5/2006

More Definitions Vertex i is a predecessor of vertex j if and only if there is a path from i to j. Vertex i is an immediate predecessor if vertex j if and only if ( i, j ) is an edge in the graph. Vertex j is a successor of vertex i if and only if there is a path from i to j. Vertex j is an immediate predecessor if vertex i if and only if ( i, j ) is an edge in the graph. The indegree of a vertex, v, is the number of edges (u, v). I.e. the number of edges that come “into” v. 5/5/2006

Topological Ordering A topological ordering of the vertices of a DAG G = (V,E) is a linear ordering such that, for vertices i, j V, if i is a predecessor of j, then i precedes j in the linear order. I.e. if there is a path from vi to vj, then vi comes before vj in the linear order In general, there is more than one topological ordering for a graph. Ex: legitimate orders to take CS courses 5/16/2006

Topological Sort void TopSort(Graph G) { unsigned int counter = 0 ; Queue<Vertex> q; Vertex indegree[|V|]; for each Vertex v { v.indegree = GetInDegree(v); if (v.indegree == 0) q.enqueue(v); } while (!q.isEmpty()) { v = q.dequeue(); Put v on the topological ordering; counter++; for (each vertex, w, adjacent to v { if (--w.indegree == 0) q.enqueue(w); if (counter != G.NumVertices()) declare an error -- G has a cycle Q: In what circumstances can counter be equal to NumVerts? A: One circumstance would be a graph with a single vertex and a directed edge from the vertex to itself.

TopSort Example 1 2 3 4 5 6 7 8 9 10 5/5/2006 Indegree array at start: 1 2 3 4 5 6 7 8 9 10 1 2 2 0 2 1 0 3 2 0 Step Queue v counter 0 1 1 4,7,10 1 2 7,10 4 2 3 10 7 3 4 1,3 10 4 5 3,5 1 5 6 5,6,2 3 6 7 6,2,9 5 7 8 2,9 6 8 9 9 2 9 10 8 9 10 5/5/2006

Running Time of TopSort At most, each vertex is enqueued just once, so there are O(|V| ) constant time queue operations. The body of the for loop is executed at most once per edges = O( |E| ) The initialization is proportional to the size of the graph if adjacency lists are used = O( |E| + |V| ) The total running time is therefore O ( |E| + |V| ) 5/5/2006