November 6, 20031 Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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 – Depth First Search ORD DFW SFO LAX
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
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 / 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.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Chapter 23: Graph Algorithms Chapter 24: Minimum Spanning Trees.
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.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
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.
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
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.
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.
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.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
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.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
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.
Liaquat Majeed Sheikh 1 1/25/2016 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.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
M. Böhlen and R. Sebastiani 9/30/20161 Data Structures and Algorithms Roberto Sebastiani
Introduction to Algorithms
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CS200: Algorithm Analysis
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Algorithms and Data Structures Lecture XI
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University

November 6, This Lecture Graphs – principles Graph representations Traversing graphs Breadth-First Search Depth-First Search Topological sort

November 6, 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 We assume directed graphs. If a graph is undirected, we represent an edge between u and v by having (u,v)  E and (v,u)  E ABCDA B CD V = {A, B, C, D} E = {(A,B), (B,A), (A,C), (C,A), (C,D), (D,C), (B,C), (C,B)}

November 6, Electronic circuits, pipeline networks Transportation and communication networks Modeling any sort of relationtionships (between components, people, processes, concepts) Applications

November 6, Graph Terminology Vertex v is adjacent to vertex u iff (u,v)  E degree (of a vertex): # of adjacent vertices Path – a sequence of vertices v 1,v 2,...v k such that v i+1 is adjacent to v i for i = 1.. k – 1

November 6, Simple path – a path with no repeated vertices Cycle – a 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 (2)

November 6, Graph Terminology (3) Subgraph – a subset of vertices and edges forming a graph Connected component – maximal connected subgraph. For example, the graph below has 3 connected components

November 6, Graph Terminology (4) (free) tree – connected graph without cycles forest – collection of trees

November 6, Data Structures for Graphs 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 6, 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(|V| 2 ) Adjacency Matrix

November 6, Pseudocode assumptions Graph ADT with an operation V():VertexSet A looping construct “for each v  V ”, where V is of a type VertexSet, and v is of a type Vertex Vertex ADT with operations: adjacent():VertexSet d():int and setd(d:int) f():int and setf(f:int) parent():Vertex and setparent(p:Vertex) color():{white, gray, black} and setcolor(c:{white, gray, black})

November 6, Graph Searching Algorithms Systematic search of every edge and vertex of the graph Graph G = (V,E) is either directed or undirected Applications Compilers Graphics Maze-solving Mapping Networks: routing, searching, clustering, etc.

November 6, 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 6, 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 6, BFS Algorithm BFS(G,s) 01 for each vertex u  G.V() 02 u.setcolor(white) 03 u.setd(  04 u.setparent(NIL) 05 s.setcolor(gray) 06 s.setd(0) 07 Q.init() 08 Q.enqueue(s) 09 while not Q.isEmpty() 10 u  Q.head() 11 for each v  u.adjacent() do 12 if v.color() = white then 13 v.setcolor(gray) 14 v.setd(u.d() + 1) 15 v.setparent(u) 16 Q.enqueue(v) 17 Q.dequeue() 18 u.setcolor(black) Init all vertices Init BFS with s Handle all u’s children before handling any children of children

November 6, Coloring of vertices 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 explored A vertex is black after all of its adjacent vertices have been discovered (the adj. list was examined completely) Lets do an example of BFS: SBCEADGF

November 6, BFS Running Time Given a graph G = (V,E) Vertices are enqueued if their 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 6, 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 6, 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 6, 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

November 6, DFS Algorithm Visit all children recursively DFS(G,s) 01 for each vertex u  G.V() 02 u.setcolor(white) 03 u.setparent(NIL) 04 time  0 05 for each vertex u  G.V() 06 if v.color() = white then DFS-Visit(v) DFS-Visit(u) 01 u.setcolor(gray) 02 time  time u.setd(time) 04 for each v  u.adjacent() 05 if v.color() = white then 06 v.setparent(u) 07 DFS-Visit(v) 08 u.setcolor(black) 09 time  time u.setf(time) Init all vertices Make sure all vertices are visited

November 6, 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 When DFS returns, every vertex u is assigned a discovery time d[u], and a finishing time f[u]

November 6, Let’s do an example of DFS:

November 6, DFS Algorithm Running Time 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 v.adjacent() the total cost for DFS-Visit is  (E) the running time of DFS is  (V+E)

November 6, Generic Graph Search BFS, when GrayVertices is a Queue DFS, when GrayVertices is a Stack GenericGraphSearch(G,s) 01 for each vertex u  G.V() 02 u.setcolor(white) 03 u.setparent(NIL) 04 s.setcolor(gray) 05 GrayVertices.init() 06 GrayVertices.add(s) 07 while not GrayVertices.isEmpty() 08 u  GrayVertices.remove() 09 for each v  u.adjacent() do 10 if v.color() = white then 11 v.setcolor(gray) 12 v.setparent(u) 13 GrayVertices.add(v) 14 u.setcolor(black)

November 6, 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 6, 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 6, DFS Parenthesis Theorem (2)

November 6, DFS Edge Classification Tree edge (gray to white) Edges in depth-first forest Back edge (gray to gray) from descendant to ancestor in depth-first tree

November 6, DFS Edge Classification (2) Forward edge (gray to black) Nontree edge from ancestor to descendant in depth-first tree Cross edge (gray to black) remainder – between trees or subtrees

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

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

November 6, DAG Theorem A directed graph G is acyclic if and only if 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 Thus, we can verify a DAG using DFS!

November 6, 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 6, Topological Sort Sorting of a directed acyclic graph (DAG) A topological sort of a DAG is a linear ordering of all its vertices such 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) 01 call DFS(G) to compute finishing times f[v] for each vertex v 02 as each vertex is finished, insert it onto the front of a linked list 03 return the linked list of vertices

November 6, 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 6, Topological Sort Running Time Running time depth-first search: O(V+E) time insert each of the |V| vertices to the front of the linked list: O(1) per insertion Thus the total running time is O(V+E)

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