CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Size: px
Start display at page:

Download "CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting."

Transcription

1 Week 5 General remarks and 2 We consider the simplest graph- algorithm, breadth-first (). We apply to compute shortest paths. Then we consider the second main graph- algorithm, depth-first (). And we consider one application of, of graphs. Reading from CLRS for week 5 5 Chapter 22, Sections 22.2, 22., The algorithm Searching through a graph is one of the most fundamental of all algorithmic tasks. () is a simple but very important technique for ing a connected graph: Such a starts from a given source vertex s and constructs a rooted spanning tree for the graph, called the breadth-first tree ( tree; the root is s). It uses a (first-in first-out) queue as its main data structure. computes the parent π[u] of each vertex u in the breadth-first tree (with the parent of the source being nil). And its distance d[u] from the source s (initialised to ), which is the length of the path from s to u in the breadth-first tree. Thus the tree contains the shortest paths from s to any other vertex, and is called an SPT. Input: A graph G with vertex set V (G) and edges represented by adjacency lists Adj. Queue A queue is a first-in-first-out data structure. (G, s) for each u V (G) 2 d[u] = π[s] = nil d[s] = 0 5 Q = (s) 6 while Q () 7 u = Dequeue[Q] 8 for each v Adj[u] 9 if d[v] = 0 d[v] = d[u] + π[v] = u 2 Enqueue(Q, v)

2 illustrated Analysis of 0/nil / / / / / / Q = () 0/nil / 2/2 / 2/2 / / Q = (, ) 0/nil / 2/2 / 2/2 / / Q = (5, 6, 7) 0/nil / 2/2 / 2/2 / / Q = (7) d[u]/π[u] u (labelling) 0/nil / / / / / / Q = (2) 0/nil / 2/2 / 2/2 / / Q = (, 5) 0/nil / 2/2 / 2/2 / / Q = (6, 7) 0/nil / 2/2 / 2/2 / / Q = () Correctness Analysis: At termination of (G, s), for every vertex v reachable from s: Time Analysis: v has been encountered; d[v] holds the length of the shortest path from s to v; π[v] represents an edge on a shortest path from v to s. The initialisation takes time Θ(V ). Each vertex is Enqueued once and Dequeued once; these queueing operations each take constant time, so the queue manipulation takes time Θ(V ) (altogether). The Adjacency list of each vertex is scanned only when the vertex is Dequeued, so scanning adjacency lists takes time Θ(E) (altogether). The overall time of is thus Θ(V + E). Background: Why do we get shortest paths? The role of the queue Is it really true that we get always shortest paths (that is, using the minimum number of edges)? Let s assume that to some vertex v there exists a shorter path P in G from s to v than found by. Let this length be d < d[v]. v s, since the distance from s to s is zero (using the path without an edge), and this is correctly computed by. 2 Consider the predecessor u on that shorter path P. If also d[u] would be wrong (that is, too big), than we could use u instead of v. Thus w.l.o.g. d[u] is correct. Now when exploring the neighbours of u, in case v is still unexplored, it would get the correct distance d = d[u] +. 5 So v must have been explored already earlier (than u). 6 So at the time of determining the distance d[u] d[v] 2, the distance d[v] must have been already set. 7 However the distances d[w] set by are non-decreasing! A crucial property of is that the distances set in step 0 of the algorithm are non-decreasing, that is, if we imagine a watch-point set at step 0, and monitor the stream of values d[v], then we will see 0,,...,, 2,..., 2,,...,,.... Why is this the case? This must be due to the queue used whose special properties we haven t yet exploited! Since in step 2, directly after setting the distance, we put the vertex into the queue, the above assertion is equivalent to the statement, that the distances of the vertices put into the queue are non-decreasing (in the order they enter the queue). Now why is this the case?

3 The role of the queue (cont.) Running on a disconnected graph First we need to notice that once a distance d[v] is set, it is never changed again. A graph G with at least one vertex s is connected if and only if (G, s) yields no d[v] =. The vertices are taken off the queue ( dequeued ) from the left, and are added ( enqueued ) to the right ( first in, first out ). What is added has a distance one more than what was taken away. We start with 0, and add some s. Then we take those s, and add 2 s. The vertices v with d[v] = are precisely the vertices not reachable from s. So for a disconnected G, running (G, s) does not yield a spanning tree for G, but a spanning tree for the component of s. Once the s are finished, we take the 2 s, and add s. And so on that why the sequence of distances is non-decreasing. For example, running on G := ({, 2,, }, { {, 2 }, {, } }) with s = we get π[] = NIL, π[2] =, π[], π[] not set. d[] = 0, d[2] =, d[] = d[] =. Restarting Now what if we need to reach all vertices? Then we need a spanning forest! Running on directed graphs We can run also on a digraph G, with start-vertex s: This means we need to For digraphs we have directed spanning trees/forests. restart on a yet unreached vertex, maintaining the old π and d values, repeating this until all vertices have been reached. Running on the previous example, first with s =, and then restarting with s =, yields 2 Only the vertices reachable from s following the directions of the edges are in that directed tree (directed from s towards the leaves). Still the paths in the directed tree, from s to any other vertex, are shortest possible (given that we obey the given directions of the edges). π[] = NIL, π[2] =, π[] = NIL, π[] =. d[] = 0, d[2] =, d[] = 0, d[] =. Note that now For a digraph there is a much higher chance, that we might need to restart if we wish to cover all vertices. d[v] is the distance of v to its root.

4 Restarts needed to have all vertices Restarts needed to have all vertices (cont.) In order to cover all vertices, Consider the simple digraph G := ({, 2, }, { (2, ), (, ) }) 2 obtaining a directed spanning forest, one needs to run at least two times, while keeping d and π (and if the first time you start it with s =, then you need to run it three times). Running (G, ), we obtain the directed tree ({ }, ) (no arc). Not covered are vertices 2, this is fine, since from vertex you can t reach them. 2 Running (G, 2), we obtain the directed tree ({, 2 }, { (2, ) }) (one arc). Not covered is vertex again fine, since from vertex 2 you can t reach vertex. We have three possibilities directed spanning forests here: ({ }, ), ({ 2 }, ), ({ }, ). 2 ({, 2 }, { (2, ) }), ({ }, ). ({, }, { (, ) }), ({ 2 }, ). Note that due to keeping the d-array between the different runs, there is no overlap. Background: Arcs of different lengths The edges of (di-)graphs have (implicitly) a length of one unit. If arbitrary non-negative lengths are allowed, then we have to generalise to Dijkstra s algorithm. This generalisation must keep the essential properties, that the distances encountered are the final ones and are non-decreasing. But now not all edges have unit-length, and thus instead of a simple queue we need to employ a priority queue. A priority queue returns the vertex in it with the smallest value (distance). () is another simple but very important technique for ing a graph. Such a constructs a spanning forest for the graph, called the depth-first forest, composed of several depth-first trees, which are rooted spanning trees of the connected components. recursively visits the next unvisited vertex, thus extending the current path as far as possible; when the gets stuck in a corner it backtracks up along the path until a new avenue presents itself. computes the parent π[u] of each vertex u in the depth-first tree (with the parent of initial vertices being nil), as well as its discovery time d[u] (when the vertex is first encountered, initialised to ) and its finishing time f [u] (when the has finished visiting its adjacent vertices).

5 The algorithm illustrated (G) for each u V (G) 2 d[u] = time = 0 for each u V (G) 5 if d[u] = 6 π[u] = nil 7 -Visit(u) -Visit(u) time = time + 2 d[u] = time for each v Adj[u] if d[v] = 5 π[v] = u 6 -Visit(v) 7 time = time + 8 f [u] = time Analysis: -Visit(u) is invoked exactly once for each vertex, during which we scan its adjacency list once. Hence, like, runs in time Θ(V + E). /nil / / / / / / d[u] f [u] /π[u] u / / / (labelling) / / / Stack = () u = Stack = () u = 2 / / / / / / /2 / / /2 / / Stack = (2, ) u = Stack = (, 2, ) u = / / / / / / /2 5 / / /2 5 / 6 /5 Stack = (,, 2, ) u = 5 Stack = (5,,, 2, ) u = 7 / / 9 / /nil 2 / / 9 / 0 /2 5 / 6 / /2 5 / 6 /5 8 7 Stack = (,, 2, ) u = 6 Stack = () Running on directed graphs What are the times good for? Again (as with ), we can run on a digraph G. Again, no longer do we obtain spanning trees of the connected components of the start vertex. But we obtain a directed spanning tree with exactly all vertices reachable from the root (when following the directions of the edges). Different from, the root (or start vertex) normally does not play a prominent role for : Thus we did not provide the form of with a start vertex as input. 2 But we provided the forest-version, which tries all vertices (in the given order) as start vertices. This will always cover all vertices, via a directed spanning forest. (Directed) trees do not contain shortest paths to that end their way of exploring a graph is too adventuresomely (while is very cautious ). Nevertheless, the information gained through the computation of discovery and finish times is very valuable for many tasks. We consider the example of scheduling later. In order to understand this example, we have first to gain a better understanding of the meaning of discovery and finishing time.

6 Existence of a path versus finishing times Background: Proof Lemma Consider a digraph G and nodes u, v V (G) with d[u] < d[v]. Then there is a path from u to v in G if and only if f [u] > f [v] holds. In words: If node u is discovered earlier than node v, then we can reach v from u iff v finishes earlier than u. If there is a path from u to v, then, since v was discovered later than u, the recursive call of -Visit(v) must happen inside the recursive call of -Visit(u) (by construction of the discovery-loop), and thus v must finish before u finishes. In the other direction, if v finishes earlier than u, then the recursive call of -Visit(v) must happen inside the recursive call of -Visit(u) (since the recursion for u must still be running). Thus, when discovering v, we must be on a path from u to v. Directed acyclic graphs An important applications of digraphs G is with scheduling: The vertices are the jobs (actions) to be scheduled. A directed edge from vertex u to vertex v means a dependency, that is, action u must be performed before action v. Now consider the situation where we have three jobs a, b, c and the following dependency digraph: G = a b Topological Given a dag G modelling a scheduling task, a basic task is to find a linear ordering of the vertices ( actions ) such that all dependencies are respected. This is modelled by the notion of. A sort of a dag is an ordering of its vertices such that for every edge (u, v), u appears before v in the ordering. For example consider Clearly this can not be scheduled! c In general we require G to by acyclic, that is, G must not contain a directed cycle. G = a b 8 c The two possible s of G are a, c, b and c, a, b. A directed acyclic graph is also called a dag.

7 Finishing times in DAGs Background: Proof There are two cases regarding the discovery times of u and v: Lemma 2 After calling on a dag, for every edge (u, v) we have f [u] > f [v]. If d[u] < d[v], then by Lemma we have f [u] > f [v] (since there is a path from u to v (of length )). 2 Now assume d[u] > d[v]. If f [u] > f [v], then we are done, and so assume that f [u] < f [v]. But then by Lemma there is a path from v to u in G, and this together with the edge from u to v establishes a cycle in G, contradicting that G is a dag. Topological via Topological illustrated Consider the result of running the algorithm on the following dag. Corollary To ly sort a dag G, we run on G and print the vertices in reverse order of finishing times. Reverse order can be obtained by putting each vertex on the front of a list as they are finished. /20 2/26 22/25 27/28 / m n o p q r s t u v w (labelling: 2/5 6/9 7/8 0/7 x y z /2 9/8 /5 d[u]/f [u]) 2/2/6 Listing the vertices in reverse order of finishing time gives the following of the vertices: u : p n o s m r y v w z x u q t f [u]:

8 The extracted directed spanning forest A directed spanning -forest (with 2 restarts) m q r t u y n o s p m n q r x o t u y s p v v x w w z z Replay the discovery and finishing times of on this forest! Note how this directed tree is developed in layers! Deciding acyclicity Revisiting the lemma for In Lemma 2 we said: For a graph G (i.e., undirected) detecting the existence of a cycle is simple, via or : G has a cycle (i.e., is not a forest) if and only if resp. will discover a vertex twice. One has to be a bit more careful here, since the parent vertex will always be discovered twice, and thus has to be excluded, but that s it. If G has no cycles, then along every edge the finishing times strictly decrease. So if we discover in digraph G an edge (u, v) with f [u] < f [v], then we know there is a cycle in G. Is this criterion also sufficient? YES: just consider a cycle, and what must happen with the finishing times on it. However for digraphs it is not that simple do you see why? Lemma Consider a digraph G. Then G has a cycle if and only if every run of on G yields some edge (u, v) E(G) with f [u] < f [v].

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting. General remarks Week 5 2 We finish, by analysing it. Then we consider the second main graph- algorithm, depth-first (). And we consider one application of, of graphs. Reading from CLRS for week 5 Chapter

More information

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms. 1 2 Week 5 3 4 5 General remarks We finish, by analysing it. Then we consider the second main graph- algorithm, depth-first (). And we consider one application of, of graphs. Reading from CLRS for week

More information

Elementary Graph Algorithms

Elementary Graph Algorithms Elementary Graph Algorithms Graphs Graph G = (V, E)» V = set of vertices» E = set of edges (V V) Types of graphs» Undirected: edge (u, v) = (v, u); for all v, (v, v) E (No self loops.)» Directed: (u, v)

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms CSE 5311 Lecture 18 Graph Algorithm Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Graphs Graph G = (V,

More information

Chapter 22. Elementary Graph Algorithms

Chapter 22. Elementary Graph Algorithms Graph Algorithms - Spring 2011 Set 7. Lecturer: Huilan Chang Reference: (1) Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 2nd Edition, The MIT Press. (2) Lecture notes from C. Y. Chen

More information

Representations of Graphs

Representations of Graphs ELEMENTARY GRAPH ALGORITHMS -- CS-5321 Presentation -- I am Nishit Kapadia Representations of Graphs There are two standard ways: A collection of adjacency lists - they provide a compact way to represent

More information

Graph Representation

Graph Representation Graph Representation Adjacency list representation of G = (V, E) An array of V lists, one for each vertex in V Each list Adj[u] contains all the vertices v such that there is an edge between u and v Adj[u]

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms s of s Computer Science & Engineering 423/823 Design and Analysis of Lecture 03 (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 29 s of s s are abstract data types that are applicable

More information

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V) Graph Algorithms Graphs Graph G = (V, E) V = set of vertices E = set of edges (V V) Types of graphs Undirected: edge (u, v) = (v, u); for all v, (v, v) E (No self loops.) Directed: (u, v) is edge from

More information

Graph Algorithms. Definition

Graph Algorithms. Definition Graph Algorithms Many problems in CS can be modeled as graph problems. Algorithms for solving graph problems are fundamental to the field of algorithm design. Definition A graph G = (V, E) consists of

More information

Graph representation

Graph representation Graph Algorithms 1 Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent for algorithms: 1. Adjacency lists. 2. Adjacency matrix. When expressing

More information

Graph: representation and traversal

Graph: representation and traversal Graph: representation and traversal CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang! Acknowledgement The set of slides have use materials from the following resources Slides for textbook

More information

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1 Graph Algorithms Chapter 22 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms

More information

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u). Parenthesis property of DFS discovery and finishing times (Thm 23.6 of CLR): For any two nodes u,v of a directed graph, if d[u] < d[v] (i.e. u discovered before v), either f[v] < f[u] (i.e. visit time

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 04 Elementary Graph Algorithms (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) sscott@cse.unl.edu Introduction

More information

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Graph Algorithms: Chapters Part 1: Introductory graph concepts UMass Lowell Computer Science 91.503 Algorithms Dr. Haim Levkowitz Fall, 2007 Graph Algorithms: Chapters 22-25 Part 1: Introductory graph concepts 1 91.404 Graph Review Elementary Graph Algorithms Minimum

More information

DFS & STRONGLY CONNECTED COMPONENTS

DFS & STRONGLY CONNECTED COMPONENTS DFS & STRONGLY CONNECTED COMPONENTS CS 4407 Search Tree Breadth-First Search (BFS) Depth-First Search (DFS) Depth-First Search (DFS) u d[u]: when u is discovered f[u]: when searching adj of u is finished

More information

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Ch 23 Traversing graphs Next: Graph Algorithms Graphs Ch 22 Graph representations adjacency list adjacency matrix Minimum Spanning Trees Ch 23 Traversing graphs Breadth-First Search Depth-First Search 11/30/17 CSE 3101 1 Graphs

More information

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019 CS 341: Algorithms Douglas R. Stinson David R. Cheriton School of Computer Science University of Waterloo February 26, 2019 D.R. Stinson (SCS) CS 341 February 26, 2019 1 / 296 1 Course Information 2 Introduction

More information

COT 6405 Introduction to Theory of Algorithms

COT 6405 Introduction to Theory of Algorithms COT 6405 Introduction to Theory of Algorithms Topic 14. Graph Algorithms 11/7/2016 1 Elementary Graph Algorithms How to represent a graph? Adjacency lists Adjacency matrix How to search a graph? Breadth-first

More information

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2 Graphs Graph G = (V, E)» V = set of vertices» E = set of edges (V V) V) Types of graphs» Undirected:

More information

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search 1 Graph Searching: Breadth-First Search Graph G =(V, E), directed or undirected with adjacency list repres. GOAL: Systematically explores

More information

Graph implementations :

Graph implementations : Graphs Graph implementations : The two standard ways of representing a graph G = (V, E) are adjacency-matrices and collections of adjacencylists. The adjacency-lists are ideal for sparse trees those where

More information

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS Taking Stock IE170: Algorithms in Systems Engineering: Lecture 16 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University February 28, 2007 Last Time The Wonderful World of This

More information

Basic Graph Algorithms

Basic Graph Algorithms Basic Graph Algorithms 1 Representations of Graphs There are two standard ways to represent a graph G(V, E) where V is the set of vertices and E is the set of edges. adjacency list representation adjacency

More information

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo

More information

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths Part VI Graph algorithms Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths 1 Chapter 22 Elementary Graph Algorithms Representations of graphs

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms s of s Computer Science & Engineering 423/823 Design and Analysis of Lecture 03 (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 29 Spring 2010 s of s s are abstract data types that

More information

Introduction to Algorithms. Lecture 11

Introduction to Algorithms. Lecture 11 Introduction to Algorithms Lecture 11 Last Time Optimization Problems Greedy Algorithms Graph Representation & Algorithms Minimum Spanning Tree Prim s Algorithm Kruskal s Algorithm 2 Today s Topics Shortest

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 31 Advanced Data Structures and Algorithms Graphs July 18, 17 Tong Wang UMass Boston CS 31 July 18, 17 1 / 4 Graph Definitions Graph a mathematical construction that describes objects and relations

More information

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search Taking Stock IE170: Algorithms in Systems Engineering: Lecture 17 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University March 2, 2007 Last Time Depth-First Search This Time:

More information

Outlines: Graphs Part-2

Outlines: Graphs Part-2 Elementary Graph Algorithms PART-2 1 Outlines: Graphs Part-2 Graph Search Methods Breadth-First Search (BFS): BFS Algorithm BFS Example BFS Time Complexity Output of BFS: Shortest Path Breath-First Tree

More information

Unit 2: Algorithmic Graph Theory

Unit 2: Algorithmic Graph Theory Unit 2: Algorithmic Graph Theory Course contents: Introduction to graph theory Basic graph algorithms Reading Chapter 3 Reference: Cormen, Leiserson, and Rivest, Introduction to Algorithms, 2 nd Ed., McGraw

More information

Single Source Shortest Path

Single Source Shortest Path Single Source Shortest Path A directed graph G = (V, E) and a pair of nodes s, d is given. The edges have a real-valued weight W i. This time we are looking for the weight and the shortest path from s

More information

CSI 604 Elementary Graph Algorithms

CSI 604 Elementary Graph Algorithms CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25 Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 4 Graphs Definitions Traversals Adam Smith 9/8/10 Exercise How can you simulate an array with two unbounded stacks and a small amount of memory? (Hint: think of a

More information

Figure 1: A directed graph.

Figure 1: A directed graph. 1 Graphs A graph is a data structure that expresses relationships between objects. The objects are called nodes and the relationships are called edges. For example, social networks can be represented as

More information

Data Structures and Algorithms. Chapter 7. Graphs

Data Structures and Algorithms. Chapter 7. Graphs 1 Data Structures and Algorithms Chapter 7 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples

More information

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 Graphs A graph, G = (V, E), consists of two sets: V is a finite non-empty set of vertices. E is a set of pairs of vertices, called

More information

22.3 Depth First Search

22.3 Depth First Search 22.3 Depth First Search Depth-First Search(DFS) always visits a neighbour of the most recently visited vertex with an unvisited neighbour. This means the search moves forward when possible, and only backtracks

More information

Graph Search. Adnan Aziz

Graph Search. Adnan Aziz Graph Search Adnan Aziz Based on CLRS, Ch 22. Recall encountered graphs several weeks ago (CLRS B.4) restricted our attention to definitions, terminology, properties Now we ll see how to perform basic

More information

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Graph G(V, E): V set of nodes (vertices); E set of edges. Notation: n = V and m = E. (Vertices are numbered

More information

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS This chapter presents methods for representing a graph and for searching a graph. Searching a graph means systematically following the edges of the graph so as to

More information

Announcements Problem Set 4 is out!

Announcements Problem Set 4 is out! CSC263 Week 8 Announcements Problem Set 4 is out! Due Tuesday (Nov 17) Other Announcements Drop date Nov 8 Final exam schedule is posted CSC263 exam Dec 11, 2-5pm This week s outline Graphs BFS Graph A

More information

Topic 12: Elementary Graph Algorithms

Topic 12: Elementary Graph Algorithms Topic 12: Elementary Graph Algorithms Pierre Flener Lars-enrik Eriksson (version of 2013-02-10) Different kinds of graphs List Tree A B C A B C Directed Acyclic Graph (DAG) D A B C D E Directed Graph (Digraph)

More information

Data Structures and Algorithms. Werner Nutt

Data Structures and Algorithms. Werner Nutt Data Structures and Algorithms Werner Nutt nutt@inf.unibz.it http://www.inf.unibz/it/~nutt Part 9 Academic Year 2011-2012 1 Acknowledgements & Copyright Notice These slides are built on top of slides developed

More information

Basic Graph Definitions

Basic Graph Definitions 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

More information

Announcements. HW3 is graded. Average is 81%

Announcements. HW3 is graded. Average is 81% CSC263 Week 9 Announcements HW3 is graded. Average is 81% Announcements Problem Set 4 is due this Tuesday! Due Tuesday (Nov 17) Recap The Graph ADT definition and data structures BFS gives us single-source

More information

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u]. Tutorial Question 1 A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first tree can also be used to classify the edges reachable from the source

More information

Graph: representation and traversal

Graph: representation and traversal Graph: representation and traversal CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Acknowledgement The set of slides have use materials from the following resources Slides for textbook

More information

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship Graph Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance or cost) path Graph Theory Many problems are mapped

More information

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456 5.4 Shortest Paths CH08-320201: Algorithms and Data Structures 456 Definitions: Path Consider a directed graph G=(V,E), where each edge e є E is assigned a non-negative weight w: E -> R +. A path is a

More information

Week 12: Minimum Spanning trees and Shortest Paths

Week 12: Minimum Spanning trees and Shortest Paths Agenda: Week 12: Minimum Spanning trees and Shortest Paths Kruskal s Algorithm Single-source shortest paths Dijkstra s algorithm for non-negatively weighted case Reading: Textbook : 61-7, 80-87, 9-601

More information

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graph. Vertex. edge. Directed Graph. Undirected Graph Module : Graphs Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS E-mail: natarajan.meghanathan@jsums.edu Graph Graph is a data structure that is a collection

More information

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review 1 Review 1 Something I did not emphasize enough last time is that during the execution of depth-firstsearch, we construct depth-first-search trees. One graph may have multiple depth-firstsearch trees,

More information

Review: Graph Theory and Representation

Review: Graph Theory and Representation Review: Graph Theory and Representation Graph Algorithms Graphs and Theorems about Graphs Graph implementation Graph Algorithms Shortest paths Minimum spanning tree What can graphs model? Cost of wiring

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms CS 5311 Lecture 19 Topological Sort Junzhou Huang, Ph.D. Department of Computer Science and ngineering CS5311 Design and Analysis of Algorithms 1 Topological Sort Want

More information

Unit 5F: Layout Compaction

Unit 5F: Layout Compaction Course contents Unit 5F: Layout Compaction Design rules Symbolic layout Constraint-graph compaction Readings: Chapter 6 Unit 5F 1 Design rules: restrictions on the mask patterns to increase the probability

More information

Unit 3: Layout Compaction

Unit 3: Layout Compaction Unit 3: Layout Compaction Course contents Design rules Symbolic layout Constraint-graph compaction Readings: Chapter 6 Unit 3 1 Design rules: restrictions on the mask patterns to increase the probability

More information

CSE 331: Introduction to Algorithm Analysis and Design Graphs

CSE 331: Introduction to Algorithm Analysis and Design Graphs CSE 331: Introduction to Algorithm Analysis and Design Graphs 1 Graph Definitions Graph: A graph consists of a set of verticies V and a set of edges E such that: G = (V, E) V = {v 0, v 1,..., v n 1 } E

More information

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10 CS 220: Discrete Structures and their Applications graphs zybooks chapter 10 directed graphs A collection of vertices and directed edges What can this represent? undirected graphs A collection of vertices

More information

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms CS43-09 Elementary Graph Algorithms Outline Representation of Graphs Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 1:30pm - 2:30pm or by appointments lifei@cs.gmu.edu with subject: CS43

More information

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms CS483-09 Elementary Graph Algorithms Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 1:30pm - 2:30pm or by appointments lifei@cs.gmu.edu with subject: CS483 http://www.cs.gmu.edu/ lifei/teaching/cs483_fall07/

More information

Shortest Path Problem

Shortest Path Problem Shortest Path Problem CLRS Chapters 24.1 3, 24.5, 25.2 Shortest path problem Shortest path problem (and variants) Properties of shortest paths Algorithmic framework Bellman-Ford algorithm Shortest paths

More information

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1 Directed Graphs Definitions. Representations. ADT s. Single Source Shortest Path Problem (Dijkstra, Bellman-Ford, Floyd-Warshall). Traversals for DGs. Parenthesis Lemma. DAGs. Strong Components. Topological

More information

W4231: Analysis of Algorithms

W4231: Analysis of Algorithms W4231: Analysis of Algorithms 10/21/1999 Definitions for graphs Breadth First Search and Depth First Search Topological Sort. Graphs AgraphG is given by a set of vertices V and a set of edges E. Normally

More information

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch] Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] 1 Directed Graphs Let V be a finite set and E a binary relation on V, that is, E VxV. Then the pair G=(V,E) is called a directed graph.

More information

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search IS 320 Introduction to lgorithms all 2014 Lecture 15 epth irst Search 1 Traversing raphs Systematic search of every edge and vertex of graph (directed or undirected) fficiency: each edge is visited no

More information

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11 Chapter 3 - Graphs Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = V, m = E. V = {

More information

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution Taking Stock IE170: Algorithms in Systems Engineering: Lecture 26 Jeff Linderoth Last Time Department of Industrial and Systems Engineering Lehigh University April 2, 2007 This Time Review! Jeff Linderoth

More information

Elementary Graph Algorithms

Elementary Graph Algorithms Elementary Graph Algorithms Representations Breadth-First Search Depth-First Search Topological Sort Strongly Connected Components CS 5633 Analysis of Algorithms Chapter 22: Slide 1 Graph Representations

More information

CSC263 Week 8. Larry Zhang.

CSC263 Week 8. Larry Zhang. CSC263 Week 8 Larry Zhang http://goo.gl/forms/s9yie3597b Announcements (strike related) Lectures go as normal Tutorial this week everyone go to BA32 (T8, F2, F2, F3) Problem sets / Assignments are submitted

More information

Lecture 3: Graphs and flows

Lecture 3: Graphs and flows Chapter 3 Lecture 3: Graphs and flows Graphs: a useful combinatorial structure. Definitions: graph, directed and undirected graph, edge as ordered pair, path, cycle, connected graph, strongly connected

More information

Lecture 11: Analysis of Algorithms (CS ) 1

Lecture 11: Analysis of Algorithms (CS ) 1 Lecture 11: Analysis of Algorithms (CS583-002) 1 Amarda Shehu November 12, 2014 1 Some material adapted from Kevin Wayne s Algorithm Class @ Princeton 1 2 Dynamic Programming Approach Floyd-Warshall Shortest

More information

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006 Trees and Graphs Basic Definitions Tree: Any connected, acyclic graph G = (V,E) E = V -1 n-ary Tree: Tree s/t all vertices of degree n+1 A root has degree n Binary Search Tree: A binary tree such that

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 7 Greedy Graph Algorithms Topological sort Shortest paths Adam Smith The (Algorithm) Design Process 1. Work out the answer for some examples. Look for a general principle

More information

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College Laura Toma Algorithms (csci2200), Bowdoin College Undirected graphs Concepts: connectivity, connected components paths (undirected) cycles Basic problems, given undirected graph G: is G connected how many

More information

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14 CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14 1 Depth first search 1.1 The Algorithm Besides breadth first search, which we saw in class in relation to Dijkstra s algorithm, there is one

More information

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Graph Traversal CSCI Algorithms I. Andrew Rosenberg Graph Traversal CSCI 700 - Algorithms I Andrew Rosenberg Last Time Introduced Graphs Today Traversing a Graph A shortest path algorithm Example Graph We will use this graph as an example throughout today

More information

Solutions to relevant spring 2000 exam problems

Solutions to relevant spring 2000 exam problems Problem 2, exam Here s Prim s algorithm, modified slightly to use C syntax. MSTPrim (G, w, r): Q = V[G]; for (each u Q) { key[u] = ; key[r] = 0; π[r] = 0; while (Q not empty) { u = ExtractMin (Q); for

More information

Dijkstra s Shortest Path Algorithm

Dijkstra s Shortest Path Algorithm Dijkstra s Shortest Path Algorithm DPV 4.4, CLRS 24.3 Revised, October 23, 2014 Outline of this Lecture Recalling the BFS solution of the shortest path problem for unweighted (di)graphs. The shortest path

More information

Lecture 10: Strongly Connected Components, Biconnected Graphs

Lecture 10: Strongly Connected Components, Biconnected Graphs 15-750: Graduate Algorithms February 8, 2016 Lecture 10: Strongly Connected Components, Biconnected Graphs Lecturer: David Witmer Scribe: Zhong Zhou 1 DFS Continued We have introduced Depth-First Search

More information

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24 /8/11 Single-Source Shortest Path Chapter 1 Shortest Paths Finding the shortest path between two nodes comes up in many applications o Transportation problems o Motion planning o Communication problems

More information

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1 Chapter 3 - Graphs Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = V, m = E. Directed

More information

TIE Graph algorithms

TIE Graph algorithms TIE-20106 1 1 Graph algorithms This chapter discusses the data structure that is a collection of points (called nodes or vertices) and connections between them (called edges or arcs) a graph. The common

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and no

More information

Final Exam. EECS 2011 Prof. J. Elder - 1 -

Final Exam. EECS 2011 Prof. J. Elder - 1 - Final Exam Ø Wed Apr 11 2pm 5pm Aviva Tennis Centre Ø Closed Book Ø Format similar to midterm Ø Will cover whole course, with emphasis on material after midterm (maps and hash tables, binary search, loop

More information

Homework No. 4 Answers

Homework No. 4 Answers Homework No. 4 Answers CSCI 4470/6470 Algorithms, CS@UGA, Spring 2018 Due Thursday March 29, 2018 There are 100 points in total. 1. (20 points) Consider algorithm DFS (or algorithm To-Start-DFS in Lecture

More information

Sample Solutions to Homework #4

Sample Solutions to Homework #4 National Taiwan University Handout #25 Department of Electrical Engineering January 02, 207 Algorithms, Fall 206 TA: Zhi-Wen Lin and Yen-Chun Liu Sample Solutions to Homework #4. (0) (a) Both of the answers

More information

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14 CS200: Graphs Rosen Ch. 9.1-9.4, 9.6, 10.4-10.5 Walls and Mirrors Ch. 14 Trees as Graphs Tree: an undirected connected graph that has no cycles. A B C D E F G H I J K L M N O P Rooted Trees A rooted tree

More information

Graph Representations and Traversal

Graph Representations and Traversal COMPSCI 330: Design and Analysis of Algorithms 02/08/2017-02/20/2017 Graph Representations and Traversal Lecturer: Debmalya Panigrahi Scribe: Tianqi Song, Fred Zhang, Tianyu Wang 1 Overview This lecture

More information

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

Algorithms and Data Structures 2014 Exercises and Solutions Week 9 Algorithms and Data Structures 2014 Exercises and Solutions Week 9 November 26, 2014 1 Directed acyclic graphs We are given a sequence (array) of numbers, and we would like to find the longest increasing

More information

Graph Algorithms Using Depth First Search

Graph Algorithms Using Depth First Search Graph Algorithms Using Depth First Search Analysis of Algorithms Week 8, Lecture 1 Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Graph Algorithms Using Depth

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 07 Single-Source Shortest Paths (Chapter 24) Stephen Scott and Vinodchandran N. Variyam sscott@cse.unl.edu 1/36 Introduction

More information

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s CSCE 750, Fall 2002 Notes 6 Page 1 10 Graph Algorithms (These notes follow the development in Cormen, Leiserson, and Rivest.) 10.1 Definitions ffl graph, directed graph (digraph), nodes, edges, subgraph

More information

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list UNIT-4 Graph: Terminology, Representation, Traversals Applications - spanning trees, shortest path and Transitive closure, Topological sort. Sets: Representation - Operations on sets Applications. 1. Name

More information

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017 CSC 172 Data Structures and Algorithms Lecture 24 Fall 2017 ANALYSIS OF DIJKSTRA S ALGORITHM CSC 172, Fall 2017 Implementation and analysis The initialization requires Q( V ) memory and run time We iterate

More information

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. Lecture 9 Graphs This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. You need to be familiar with the design and use of basic data structures such as Lists, Stacks,

More information

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M. Lecture 9 Graphs Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M. Wootters (2017) 1 Graphs A graph is a set of vertices

More information

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting CS2 Algorithms and Data Structures Note 10 Depth-First Search and Topological Sorting In this lecture, we will analyse the running time of DFS and discuss a few applications. 10.1 A recursive implementation

More information