November 19, 20011 Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University

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

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 11 DFS Properties of DFS Topological sort.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Shortest Path Problems
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
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.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Graphs and Paths : Chapter 15 Saurav Karmakar
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Chapter 05 Introduction to Graph And Search Algorithms.
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
BCA-II Data Structure Using C Submitted By: Veenu Saini
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CS200: Algorithm Analysis
Graphs Breadth First Search & Depth First Search
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Basic Graph Algorithms
Algorithms and Data Structures Lecture XI
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University

November 19, This Lecture Graphs – principles Graph representations adjacency list adjacency matrix Traversing graphs Breadth-First Search Depth-First Search Topological Sort

November 19, Graphs – Definition A graph G = (V,E) is composed of: V: set of vertices E  V  V: set of edges connecting the vertices An edge e = (u,v) is a pair of vertices (u,v) is ordered, if G is a directed graph

November 19, Electronic circuits: find the path with the least resitence to AD E01 Networks: roads, flights, communication, etc. Applications AD E01

November 19, Graph Terminology adjacent vertices: connected by an edge degree (of a vertex): # of adjacent vertices path: sequence of vertices v 1,v 2,...v k such that consecutive vertices v i and v i+1 are adjacent Since adjacent vertices each count the adjoining edge, it will be counted twice

November 19, Graph Terminology (2) simple path: no repeated vertices

November 19, cycle: simple path, except that the last vertex is the same as the first vertex connected graph: any two vertices are connected by some path Graph Terminology (3)

November 19, Graph Terminology (4) subgraph: subset of vertices and edges forming a graph connected component: maximal connected subgraph. E.g., the graph below has 3 connected components

November 19, Graph Terminology (5) (free) tree - connected graph without cycles forest - collection of trees

November 19, Data Structures for Graphs How can we represent a graph? To start with, we can store the vertices and the edges into two containers, and we store with each edge object references to its start and end vertices

November 19, Edge List The edge list Easy to implement Finding the edges incident on a given vertex is inefficient since it requires examining the entire edge sequence

November 19, Adjacency List The Adjacency list of a vertex v: a sequence of vertices adjacent to v Represent the graph by the adjacency lists of all its vertices

November 19, Matrix M with entries for all pairs of vertices M[i,j] = true – there is an edge (i,j) in the graph M[i,j] = false – there is no edge (i,j) in the graph Space = O(n 2 ) Adjacency Matrix

November 19, Graph Searching Algorithms Systematic search of every edge and vertex of the graph Graph G = (V,E) is either directed or undirected Today's algorithms assume an adjacency list representation Applications Compilers Graphics Maze-solving Mapping Networks: routing, searching, clustering, etc.

November 19, Breadth First Search A Breadth-First Search (BFS) traverses a connected component of a graph, and in doing so defines a spanning tree with several useful properties BFS in an undirected graph G is like wandering in a labyrinth with a string. The starting vertex s, it is assigned a distance 0. In the first round, the string is unrolled the length of one edge, and all of the edges that are only one edge away from the anchor are visited (discovered), and assigned distances of 1

November 19, Breadth-First Search (2) In the second round, all the new edges that can be reached by unrolling the string 2 edges are visited and assigned a distance of 2 This continues until every vertex has been assigned a level The label of any vertex v corresponds to the length of the shortest path (in terms of edges) from s to v

November 19, BFS Algorithm BFS(G,s) 01 for each vertex u  V[G]-{s} 02 color[u]  white 03 d[u]   04  [u]  NIL 05 color[s] gray 06 d[s]  0 07  [u]  NIL 08 Q  {s} 09 while Q  do 10 u  head[Q] 11 for each v  Adj[u] do 12 if color[v] = white then 13 color[v] gray 14 d[v]  d[u]  [u]  u 16 Enqueue(Q,v) 17 Dequeue(Q) 18 color[u]  black Init all vertices Init BFS with s Handle all u’s children before handling any children of children

November 19, BFS Example    rsu  t  wvyx 0 s Q     rsu  t  wvyx 1 w 1 r Q     rsu  t  wvyx 2 t 1 r 2 x Q     rsu  t  wvyx 2 x 2 t 2 v Q

November 19, BFS Example     rsu  t  wvyx 2 v 2 x 3 u Q     rsu  t  wvyx 3 u 2 v 3 y Q     rsu  t  wvyx 3 y 3 u Q     rsu  t  wvyx 3 y Q

November 19, BFS Example: Result     rsu  t  wvyx - Q

November 19, BFS Running Time Given a graph G = (V,E) Vertices are enqueued if there color is white Assuming that en- and dequeuing takes O(1) time the total cost of this operation is O(V) Adjacency list of a vertex is scanned when the vertex is dequeued (and only then…) The sum of the lengths of all lists is  (E). Consequently, O(E) time is spent on scanning them Initializing the algorithm takes O(V) Total running time O(V+E) (linear in the size of the adjacency list representation of G)

November 19, BFS Properties Given a graph G = (V,E), BFS discovers all vertices reachable from a source vertex s It computes the shortest distance to all reachable vertices It computes a breadth-first tree that contains all such reachable vertices For any vertex v reachable from s, the path in the breadth first tree from s to v, corresponds to a shortest path in G

November 19, Breadth First Tree Predecessor subgraph of G G  is a breadth-first tree V  consists of the vertices reachable from s, and for all v  V , there is a unique simple path from s to v in G  that is also a shortest path from s to v in G The edges in G   are called tree edges

November 19, Depth-First Search A depth-first search (DFS) in an undirected graph G is like wandering in a labyrinth with a string and a can of paint We start at vertex s, tying the end of our string to the point and painting s “visited (discovered)”. Next we label s as our current vertex called u Now, we travel along an arbitrary edge (u,v). If edge (u,v) leads us to an already visited vertex v we return to u If vertex v is unvisited, we unroll our string, move to v, paint v “visited”, set v as our current vertex, and repeat the previous steps

November 19, Depth-First Search (2) Eventually, we will get to a point where all incident edges on u lead to visited vertices We then backtrack by unrolling our string to a previously visited vertex v. Then v becomes our current vertex and we repeat the previous steps Then, if all incident edges on v lead to visited vertices, we backtrack as we did before. We continue to backtrack along the path we have traveled, finding and exploring unexplored edges, and repeating the procedure

November 19, Init all vertices DFS Algorithm Visit all children recursively

November 19, DFS Algorithm (2) Initialize – color all vertices white Visit each and every white vertex using DFS-Visit Each call to DFS-Visit(u) roots a new tree of the depth-first forest at vertex u A vertex is white if it is undiscovered A vertex is gray if it has been discovered but not all of its edges have been discovered A vertex is black after all of its adjacent vertices have been discovered (the adj. list was examined completely)

November 19, DFS Algorithm (3) When DFS returns, every vertex u is assigned a discovery time d[u], and a finishing time f[u] Running time the loops in DFS take time  (V) each, excluding the time to execute DFS-Visit DFS-Visit is called once for every vertex its only invoked on white vertices, and paints the vertex gray immediately for each DFS-visit a loop interates over all Adj[v] the total cost for DFS-Visit is  (E) the running time of DFS is  (V+E)

November 19, DFS Example u x vw yz 1/ u x vw yz 2/ u x vw yz 1/ 2/ 3/ u x vw yz 1/ 2/ 3/4/ u x vw yz 1/ 2/ 3/4/ B u x vw yz 1/ 2/ 3/4/5 B

November 19, DFS Example (2) u x vw yz 1/ 2/ 3/64/5 B u x vw yz 1/ 2/7 3/64/5 B u x vw yz 1/ 2/7 3/64/5 B F u x vw yz 1/8 2/7 3/64/5 B F u x vw yz 1/8 2/7 3/64/5 B F 9/ u x vw yz 1/8 2/7 3/64/5 B F 9/ C

November 19, DFS Example (3) u x vw yz 1/8 2/7 3/64/5 B F 9/ C 10/ u x vw yz 1/8 2/7 3/64/5 B F 9/ C 10/ B u x vw yz 1/8 2/7 3/64/5 B F 9/ C 10/11 B u x vw yz 1/8 2/7 3/64/5 B F 9/12 C 10/11 B

November 19, Predecessor Subgraph Define slightly different from BFS The PD subgraph of a depth-first search forms a depth-first forest composed of several depth-first trees The edges in G   are called tree edges

November 19, DFS Timestamping The DFS algorithm maintains a monotonically increasing global clock discovery time d[u] and finishing time f[u] For every vertex u, the inequality d[u] < f[u] must hold

November 19, DFS Timestamping Vertex u is white before time d[u] gray between time d[u] and time f[u], and black thereafter Notice the structure througout the algorithm. gray vertices form a linear chain correponds to a stack of vertices that have not been exhaustively explored (DFS-Visit started but not yet finished)

November 19, DFS Parenthesis Theorem Discovery and finish times have parenthesis structure represent discovery of u with left parenthesis "(u" represent finishin of u with right parenthesis "u)" history of discoveries and finishings makes a well- formed expression (parenthesis are properly nested) Intuition for proof: any two intervals are either disjoint or enclosed Overlaping intervals would mean finishing ancestor, before finishing descendant or starting descendant without starting ancestor

November 19, DFS Parenthesis Theorem (2)

November 19, DFS Edge Classification Tree edge (gray to white) encounter new vertices (white) Back edge (gray to gray) from descendant to ancestor

November 19, DFS Edge Classification (2) Forward edge (gray to black) from ancestor to descendant Cross edge (gray to black) remainder – between trees or subtrees

November 19, DFS Edge Classification (3) Tree and back edges are important Most algorithms do not distinguish between forward and cross edges

November 19, Directed Acyclic Graphs A DAG is a directed graph with no directed cycles Often used to indicate precedences among events, i.e., event a must happen before b An example would be a parallel code execution Inducing a total order can be done using Topological Sorting

November 19, DAG Theorem A directed graph G is acyclic iff a DFS of G yields no back edges Proof suppose there is a back edge (u,v); v is an ancestor of u in DFS forest. Thus, there is a path from v to u in G and (u,v) completes the cycle suppose there is a cycle c; let v be the first vertex in c to be discovered and u is a predecessor of v in c. Upon discovering v the whole cycle from v to u is white We must visit all nodes reachable on this white path before return DFS-Visit(v), i.e., vertex u becomes a descendant of v Thus, (u,v) is a back edge

November 19, Topological Sort Sorting of a directed acyclic graph (DAG) A topological sort of a DAG is a linear ordering of all its vertices sucht that for any edge (u,v) in the DAG, u appears before v in the ordering The following algorithm topologically sorts a DAG The linked lists comprises a total ordering Topological-Sort(G) 1) call DFS(G) to compute finishing times f[v] for each vertex v 2) as each vertex is finished, insert it onto the front of a linked list 3) return the linked list of vertices

November 19, Topological Sort Example Precedence relations: an edge from x to y means one must be done with x before one can do y Intuition: can schedule task only when all of its subtasks have been scheduled

November 19, Topological Sort Running time depth-first search: O(V+E) time insert each of the |V| vertices onto the front of the linked list: O(1) per insertion Thus the total running time is O(V+E)

November 19, Topological Sort Correctness Claim: for a DAG, an edge When (u,v) explored, u is gray. We can distinguish three cases v = gray  (u,v) = back edge (cycle, contradiction) v = white  v becomes descendant of u  v will be finished before u  f[v] < f[u] v = black  v is already finished  f[v] < f[u] The definition of topological sort is satisfied

November 19, Next Lecture Graphs: Minimum Spanning Trees Greedy algorithms