Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1
|
|
- Amice Douglas
- 5 years ago
- Views:
Transcription
1 Graph Algorithms Chapter 22 CPTR 430 Algorithms Graph Algorithms
2 Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms for such an obscure area? As it turns out, many practical problems can be solved by applying some basic graph algorithms CPTR 430 Algorithms Graph Algorithms 2
3 Notation A graph is a mathematical structure that consists of a set of vertices and a set of edges G G E V is the number of vertices E is the number of edges In the context of asymptotic notation, V means V and E means E For example, O V 2 really means O V 2 CPTR 430 Algorithms Graph Algorithms 3
4 Graph Representation Adjacency matrix Adjacency list CPTR 430 Algorithms Graph Algorithms 4
5 Undirected Graph Representations CPTR 430 Algorithms Graph Algorithms 5
6 Directed Graph Representations CPTR 430 Algorithms Graph Algorithms 6
7 Which Representation is Better? Adjacency lists are good for sparse graphs Sparse graph: E V 2 The list nodes could instead contain pointers to vertices For digraphs: sum of lengths of the lists = For undirected graphs: sum of lengths of the lists = 2 Space required: Θ V E Adjacency matrix is better for dense graphs Dense graph: E is not mush less than Can quickly tell if two vertices share an edge Space required: Θ V 2, independent of the number of edges Undirected graphs can use a triangular matrix Unweighted graphs require only one bit per edge! V E 2 E CPTR 430 Algorithms Graph Algorithms 7
8 Weighted Graphs For each u v E, w : E w u v is the weight of edge u v Weighted graphs represent edge costs, lengths, time, etc. Adjacency list: the edge weight is stored in the node for the edge in the list Adjacency matrix: the edge weight is stored in matrix CPTR 430 Algorithms Graph Algorithms 8
9 Breadth-first Search Used in a number of classic graph algorithms: Prim s minimal spanning tree Dijkstra s single source shortest path Finds all the vertices reachable from a source vertex, s Produces a breadth-first tree whose root is s Computes the length of the path to each reachable vertex Path length = number of edges in the path Breadth-first all the vertices at distance k are located before any reachable vertices farther than distance k are found CPTR 430 Algorithms Graph Algorithms 9
10 Coloring the Vertices BFS marks vertices to manage its progress Vertices are: Colored white initially Colored gray when first discovered Colored black when all their neighbors have been discovered u v E and u is black v is either gray or black Gray vertices represent the frontier between discovered and undiscovered vertices CPTR 430 Algorithms Graph Algorithms 0
11 Breadth-first Tree BFS essentially builds a tree The tree s root is the source vertex Vertex v discovered while scanning vertex u neighbors has an edge from parent u to child v the tree CPTR 430 Algorithms Graph Algorithms
12 Implementation Particulars Augment vertices with additional information: color (black, gray, white) (color) predecessor (parent vertex in the BF tree), π v (predecessor) distance (number of edges from root [source vertex] to this vertex), d v (distance) Use a queue to manage the gray vertices CPTR 430 Algorithms Graph Algorithms 2
13 How It Works First, for each vertex: set its color to white (undiscovered) set its predecessor to null (not in the BF tree) set its distance to (may not even be a path to it from the source vertex) For the start vertex s: color s gray set d s to 0 place s in the queue CPTR 430 Algorithms Graph Algorithms 3
14 How It Works (cont.) As long as the queue is not empty, extract a vertex u from the queue and: for each vertex v adjacent to u: color v gray set d v set π v to d u to u Place v into the queue Color u black CPTR 430 Algorithms Graph Algorithms 4
15 Analysis of Running Time Use aggregate analysis Every vertex must be colored and properly initialized the running time for initialization is O V After initialization, no vertex is ever made white again each vertex is enqueued at most once Unreachable vertices will not be enqueued Recall that the time to enqueue and dequeue elements is O all queue operations take O V time CPTR 430 Algorithms Graph Algorithms 5
16 Analysis of Running Time (cont.) Each time a vertex is dequeued: its adjacent vertices (via its adjacency list) are each are checked to see if they need to be enqueued (i.e., are white) a vertex s adjacency list is checked once only the sum of the vertices in all adjacency lists is Θ And entry in an adjacency list represents an edge The time spent scanning the adjacency lists is O the total time for breadth-first search is E E O V O V O E O V E This means that BFS runs in time linear in the size of the (complete) adjacency list representation of the graph CPTR 430 Algorithms Graph Algorithms 6
17 Shortest Path Define δ s v to be the shortest distance from vertex s to vertex v The minimum number of edges in any path from s to v δ s v no path exists between s and v u v E δ s v δ s u (Lemma 22.) u is reachable from s v is reachable from s u u u The shortest path from s to v cannot be longer than the shortest path from s to u followed by edge u v δ s v δ s u is unreachable from s δ s δ s v δ s CPTR 430 Algorithms Graph Algorithms 7
18 Shortest Path Property of BFS For any directed or undirected graph G distance attribute of each vertex v V V E, BFS assigns δ s v the Proof: Lemma 22.2 First, show something weaker: v V d v δ s v Proceed by induction on the number of enqueue operations Basis case: Is it true after the first enqueue operation? Inductive step: True after k enqueue operations true after k enqueue operations? CPTR 430 Algorithms Graph Algorithms 8
19 Basis Step This is the case where s, the source vertex, is enqueued Due to the initialization part of the algorithm: d s 0 δ s s and v V s d v δ s v In either situation, v V d v δ s v CPTR 430 Algorithms Graph Algorithms 9
20 Inductive Step A vertex is enqueued only if it is white Let vertex v be a white vertex discovered while considering vertex u s neighbors I.e., u v E, and v is white Vertex v must be enqueued The inductive step is then d v d u δ s u δ s v Assignment in BFS I.H. Lemma 22. Once enqueued, v s color is set to gray, so it can never be enqueued again d v is never changed again, and the I.H. is maintained CPTR 430 Algorithms Graph Algorithms 20
21 Lemma 22.3 Let v v r v2 be the vertices, in order, in the queue during some part of the execution of BFS on graph v is the head of the queue v r is the tail of the queue V E d v r d v i r 2 d v i d v i CPTR 430 Algorithms Graph Algorithms 2
22 Proof of Lemma 22.3 Proceed by induction of the number of queue operations (not just the number of enqueue operations) Basis case: Does the lemma hold after the first queue operation? Inductive step: True after k queue operations true after k queue operations? CPTR 430 Algorithms Graph Algorithms 22
23 Basis Case The first queue operation is always enqueuing s, the source vertex Here, r d v r d v i : d s d v i d v d v holds vacuously CPTR 430 Algorithms Graph Algorithms 23
24 Inductive Step We must show it holds for both enqueue and dequeue operations Queue head v dequeued v 2 becomes the new head If the queue becomes empty then Lemma 22.3 holds vacuously d v d v 2, by the I.H. d v r d v d v 2, and the other inequalities at unaffected CPTR 430 Algorithms Graph Algorithms 24
25 Inductive Step (cont.) When vertex v is enqueued, it becomes v r Vertex v is enqueued while scanning vertex u s neighbors u v E Vertex u was the queue head, but it was dequeued before its neighbors are scanned By the I.H., the new head v has d v d v r d v d u d v d u By the I.H., d v r d v r d u d u d v d v r and the other inequalities are unaffected, CPTR 430 Algorithms Graph Algorithms 25
26 Corollary 22.4 Lemma 22.3 leads to Corollary 22.4: For any two vertices v i and v j that are enqueued during the execution of BFS where v i is enqueued before v j, d v i d v j when v j is enqueued. Thus vertices are enqueued with montonically increasing d values over time CPTR 430 Algorithms Graph Algorithms 26
27 BFS Correctness For a graph G BFS: V E (directed or undirected), and source vertex s V, discovers every vertex v V that is reachable from s properly assigns the d attribute of vertices, such that from s, d v δ s v v V reachable finds a shortest path from s to any reachable vertex v: s to π v by edge π v v (where v s) followed CPTR 430 Algorithms Graph Algorithms 27
28 BFS Correctness Proof Proceed by contradiction Assume some vertex is assigned an incorrect d value (i.e., its d attribute length of shortest path to it from s) Let v be the vertex with a minimum δ (d v δ s v ) s v with an incorrect d value v s (Why?) d v δ d v s v (Lemma 22.2) and d v δ s v δ s v v is reachable from s; otherwise, δ s v δ s v d v CPTR 430 Algorithms Graph Algorithms 28
29 BFS Correctness Proof (cont.) Let u be the vertex immediately preceding v on a shortest path from s to v δ s v δ s u δ s u d u δ s v δ s u and s has a minimum δ s v with incorrect d It follows from all of the above that d v δ s v δ s u d u We will use these relationships to demonstrate a contradiction CPTR 430 Algorithms Graph Algorithms 29
30 BFS Correctness Proof (cont.) When vertex u is dequeued, v is either white (undiscovered), gray (discovered) or black (closed): Suppose v is white The BFS algorithm sets d v d v d u (previous slide) d u, contradicting the fact that Suppose v is black v has already been removed from the queue, (Corollary 22.4) Again, this contradicts d v d u so d v (previous slide) d u CPTR 430 Algorithms Graph Algorithms 30
31 BFS Correctness Proof (cont.) Suppose v is gray v was colored gray because its is adjacent to some other vertex, w, that was dequeued at some time before u is dequeued Since it is adjacent to w, d v d w d u Rightarrow d v d u (Corollary 22.4) d w, again the same contradiction Therefore, v V d v δ s v π v δ u s v d v d u δ s π v plus the edge π v v CPTR 430 Algorithms Graph Algorithms 3
32 Depth-first Search Search as deep as possible instead of visiting all neighboring vertices first (as BFS does) The neighbors of the most recently discovered vertex are considered first BFS is queue based; DFS is stack based The stack in maintained implicitly via recursion CPTR 430 Algorithms Graph Algorithms 32
33 DFS Algorithm Particulars The DFS algorithm in your book uses the three colors (white, gray, black) as in BFS Two timestamps are used (d and f ): d is the time the vertex is discovered (grayed) f is the time the vertex is finished (backened) The two different timestamps are used for reasoning about the behavior of DFS CPTR 430 Algorithms Graph Algorithms 33
34 dfsvisit(): DFS Running Time As in BFS, use aggregate analysis DFS calls dfsvisit() exactly once for every vertex in the graph it is O V not counting the time for the calls of dfsvisit() dfsvisit() is invoked only on unmarked vertices and it immediately marks any vertex it processes dfsvisit() is invoked only on unmarked vertices (or white vertices) CPTR 430 Algorithms Graph Algorithms 34
35 DFS Running Time (cont.) The loop in dfsvisit(v) scans the vertices adjacent to v; if adj v the set of vertices adjacent to v, then the loop takes Θ adj v vertex v The total time for all executions of dfsvisit() is thus is time for v V adj v Θ E DFS therefore takes Θ V E time The simpler DFS takes just Θ E time CPTR 430 Algorithms Graph Algorithms 35
36 Parenthesis structure E If a DFS is performed on G V, then for any two vertices u one the following three conditions hold: v V exactly The intervals d u f u and d v f v is a descendent of the other in a depth-first forest are disjoint, and neither vertex The interval d u f u d v f v, and u is a descendent of v in a depth-first tree is contained entirely within the interval The interval d v f v d u f u, and v is a descendent of u in a depth-first tree is contained entirely within the interval CPTR 430 Algorithms Graph Algorithms 36
37 White Path Theorem In a depth-first forest of graph G V, vertex v is a descendent of vertex u iff at time d u (the time DFS discovers u) v can be reached from u following a path consisting entirely of white vertices E See proof on Page 545 CPTR 430 Algorithms Graph Algorithms 37
38 DFS Edge Classification Tree edges edges in the depth-first forest G π Back edges edges u v depth-first tree (self-loops are back edges) connecting a vertex u to an ancestor v in a Forward edges non-tree edges descendent v in a depth-first tree u v connecting a vertex u to a Cross edges all other edges, e.g.: connect two vertices that where neither is an ancestor of the other connect vertices in different depth-first trees CPTR 430 Algorithms Graph Algorithms 38
39 Using DFS to Classify Edges DFS can be modified to classify edge types as it proceeds Color an edge first explored white edge gray edge black edge u v based on the color of vertex v when the edge is tree edge back edge cross edge or forward edge CPTR 430 Algorithms Graph Algorithms 39
40 Edges in Undirected Graphs Edges u v and v u are the same edge in undirected graphs The classification chosen is the one appropriate for the first edge encountered If If u v color v u color is encountered first, then both is encountered first, then both u v u v and and v u v u are given v s are given u s Undirected graphs contain no forward or cross edges CPTR 430 Algorithms Graph Algorithms 40
41 Theorem 22.0 In a DFS of an undirected graph G, every edge of G is either a tree edge or a back edge Let u v be an arbitrary edge in G Suppose w.l.g. that d u d v v must be discovered and finished before we finish u (i.e., u is still gray) since v is on u s adjacency list Edge u v explored first in the direction from u to v v is undiscovered (white) until then, since otherwise the edge would already been explored in the direction from v to u u v becomes a tree edge Edge u v explored first in the direction from v to u u is still gray when the edge is explored u v is a back edge, CPTR 430 Algorithms Graph Algorithms 4
42 Topological Sort A linear ordering of all the vertices of a directed acyclic graph (dag) G such that if u v is an edge in G, then u appears before v in the ordering In a topological sort you can arrange all the vertices in a line with all edges pointing pointing from left to the right Dags are useful for scheduling and prioritizing events A topological sort allows proper ordering for single thread execution CPTR 430 Algorithms Graph Algorithms 42
43 Dags Have No Cycles A directed graph G is acyclic A depth-first search of G yields no back edges Suppose there is a back edge u v. It follows that vertex v is an ancestor of vertex u in the depth-first forest. Thus, there is a path from v to u in G, and the back edge u v completes a cycle. Suppose that G contains a cycle c. Let v be the first vertex discovered in c, and let u v be the edge in c before v. At time d v the vertices of c form a path of white vertices from v to u. According to the whitepath theorem, u will be a descendent of v in the depth-first forest. Thus, u v is a back edge. CPTR 430 Algorithms Graph Algorithms 43
44 Topological Sort Algorithm public Vector toposort(graph G) { Vector topolist = new Vector(); Vertex.makeMark(); for ( Vertex u in G.vertices ) if (!u.ismarked() ) dfsvisit(u, topolist); return topolist; } private void dfsvisit(vertex u, Vector topolist) { u.mark(); for ( Vertex v in u.adjacents ) if (!v.ismarked() ) dfsvisit(v); topolist.insertelementat(u, 0); } CPTR 430 Algorithms Graph Algorithms 44
45 Topological Sort Correctness To prove the correctness of topological sort we must show that when DFS is performed on a dag G V E, if u v E, then f u f v Consider edge u v explored by DFS v cannot be gray because v gray v is white v is black v is an ancestor of u u v is a back edge violating Lemma 22. v is black or white v is a descendent of u f u f v v is already finished We are exploring from u u is not finished u is not yet timestamped for f f v f u CPTR 430 Algorithms Graph Algorithms 45
46 Topological Sort Running Time DFS takes time O E Insertion onto the front of a linked list takes O V vertices time for each of the The total time for the topological sort is O V E This is time linear in the number of edges and vertices CPTR 430 Algorithms Graph Algorithms 46
47 Strongly Connected Components V E V such that for every pair of vertices u A strongly connected component of a digraph G set of vertices C have u v and v u G v is reachable from u and v is reachable from u is the transpose of graph G E is a maximal v C, we G V, where E u v E E is the set of edges in G with the directions reversed Given an adjacency list representation of G, the time to create G O V E v To compute the strongly connected components of digraph G, perform two depth-first searches: one on G, the other on G Running time: Θ V E u is CPTR 430 Algorithms Graph Algorithms 47
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 informationGraph 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 informationDFS & 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 informationElementary 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 informationCSI 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 informationGraph: 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 informationDesign 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 informationCHAPTER 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 informationGraphs. 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 informationCOT 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 informationCSCE 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 informationGraph 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 informationCS 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 informationElementary 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 informationChapter 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 informationW4231: 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 informationBasic 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 informationData 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 informationFigure 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 informationChapter 22: Elementary Graph Algorithms. Definitions:
Chapter 22: Elementary Graph Algorithms. Definitions: 1. G = (V, E), where V is a set of points and E is a set of edges connecting point-pairs from V. 2. We use V synonymously with V and E with E when
More informationAnnouncements. 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 informationGraph 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 informationGraph 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 informationRepresentations 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 informationJana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides
Jana Kosecka Red-Black Trees Graph Algorithms Many slides here are based on E. Demaine, D. Luebke slides Binary Search Trees (BSTs) are an important data structure for dynamic sets In addition to satellite
More informationAlgorithm 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 informationCS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.
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
More informationComputer 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 informationCS 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 informationTopic 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 informationGraph 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 informationDesign 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 informationCS 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 informationMinimum 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 informationLecture 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 informationCS 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 informationGraph 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 informationUndirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1
Undirected Graphs Terminology. Free Trees. Representations. Minimum Spanning Trees (algorithms: Prim, Kruskal). Graph Traversals (dfs, bfs). Articulation points & Biconnected Components. Graph Matching
More informationStrongly Connected Components
Strongly Connected Components Let G = (V, E) be a directed graph Write if there is a path from to in G Write if and is an equivalence relation: implies and implies s equivalence classes are called the
More informationLecture 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 information22.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 informationWeek 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 informationData 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 informationGraph 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 informationAlgorithm 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 informationGraph: 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 informationSlides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai
Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai Mini-HW 7 released Due on 11/30 (Thur) 17:20 Homework 3 released tonight Due on 12/14 (Thur) 17:20 (three weeks) Class change!! Start from
More informationComputer 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 informationCS 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 informationTaking 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 informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 5 Exploring graphs Adam Smith 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Puzzles Suppose an undirected graph G is connected.
More informationCS521 \ Notes for the Final Exam
CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )
More informationCS 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 informationGraphs. 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 informationGraphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington
Graphs CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington 1 Representation Adjacency matrix??adjacency lists?? Review Graphs (from CSE 2315)
More informationCS2 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 informationUndirected 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 informationGraph. 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 informationCopyright 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 informationTutorial. 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 informationCS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department
CS473-Algorithms I Lecture 3-A Graphs Graphs A directed graph (or digraph) G is a pair (V, E), where V is a finite set, and E is a binary relation on V The set V: Vertex set of G The set E: Edge set of
More information- Logic and Algorithms - Graph Algorithms
Fundamentals of Computer Sience and Digital Communications - Logic and Algorithms - Graph Algorithms Johan Larsson Marco Loh 22..24 Overview What is a Graph? Representations of Graphs Breadth-first Search
More informationProof: 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 informationCSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms
Chapter 3 CSE 417: Algorithms and Computational Complexity Graphs Reading: 3.1-3.6 Winter 2012 Graphs and Graph Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.
More informationData 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 informationTaking 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(Re)Introduction to Graphs and Some Algorithms
(Re)Introduction to Graphs and Some Algorithms Graph Terminology (I) A graph is defined by a set of vertices V and a set of edges E. The edge set must work over the defined vertices in the vertex set.
More informationShortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search
Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search 6.082 Fall 2006 Shortest Path Routing, Slide 1 Routing in an
More informationGraphs. 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 informationPart 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 informationIntroduction 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 informationCISC 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 informationFundamental Graph Algorithms Part Four
Fundamental Graph Algorithms Part Four Announcements Problem Set One due right now. Due Friday at 2:15PM using one late period. Problem Set Two out, due next Friday, July 12 at 2:15PM. Play around with
More informationScribes: 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 informationFinding Strongly Connected Components
Yufei Tao ITEE University of Queensland We just can t get enough of the beautiful algorithm of DFS! In this lecture, we will use it to solve a problem finding strongly connected components that seems to
More informationUNIT 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 informationHomework 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 informationAlgorithms: Lecture 10. Chalmers University of Technology
Algorithms: Lecture 10 Chalmers University of Technology Today s Topics Basic Definitions Path, Cycle, Tree, Connectivity, etc. Graph Traversal Depth First Search Breadth First Search Testing Bipartatiness
More informationStrongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.
Directed Graph In a directed graph, each edge (u, v) has a direction u v. Thus (u, v) (v, u). Directed graph is useful to model many practical problems (such as one-way road in traffic network, and asymmetric
More informationGraph 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 informationLecture 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 informationDepth-First Search Depth-first search (DFS) is another way to traverse the graph.
Depth-First Search Depth-first search (DFS) is another way to traverse the graph. Motivating example: In a video game, you are searching for a path from a point in a maze to the exit. The maze can be modeled
More informationSolutions 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 informationChapter 22 Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms Graph Representations Graph G = (V,E) Directed Undirected Adjacency Lists Adjacency Matrix Graph Representations Adjacency List: Undirected Memory: Adjacency: Graph
More informationECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm
ECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm Ladan Tahvildari, PEng, SMIEEE Associate Professor Software Technologies Applied Research (STAR) Group Dept. of
More informationComputer 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 informationOutlines: 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 informationTopics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302
Topics VCU, Department of Computer Science CMSC 302 Trees Vojislav Kecman Terminology Trees as Models Some Tree Theorems Applications of Trees Binary Search Tree Decision Tree Tree Traversal Spanning Trees
More informationCSE 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 informationKonigsberg Bridge Problem
Graphs Konigsberg Bridge Problem c C d g A Kneiphof e D a B b f c A C d e g D a b f B Euler s Graph Degree of a vertex: the number of edges incident to it Euler showed that there is a walk starting at
More information( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&
CSE 0 Name Test Summer 008 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time for the following code is in which set? for (i=0; i
More informationLecture 9 Graph Traversal
Lecture 9 Graph Traversal Euiseong Seo (euiseong@skku.edu) SWE00: Principles in Programming Spring 0 Euiseong Seo (euiseong@skku.edu) Need for Graphs One of unifying themes of computer science Closely
More informationCS473-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 informationPractical Session No. 12 Graphs, BFS, DFS, Topological sort
Practical Session No. 12 Graphs, BFS, DFS, Topological sort Graphs and BFS Graph G = (V, E) Graph Representations (V G ) v1 v n V(G) = V - Set of all vertices in G E(G) = E - Set of all edges (u,v) in
More informationLecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1
CME 305: Discrete Mathematics and Algorithms Instructor: Professor Aaron Sidford (sidford@stanford.edu) January 11, 2018 Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1 In this lecture
More informationDirected 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 informationInf 2B: Graphs II - Applications of DFS
Inf 2B: Graphs II - Applications of DFS Kyriakos Kalorkoti School of Informatics University of Edinburgh Reminder: Recursive DFS Algorithm dfs(g) 1. Initialise Boolean array visited by setting all entries
More information3.1 Basic Definitions and Applications
Graphs hapter hapter Graphs. Basic efinitions and Applications Graph. G = (V, ) n V = nodes. n = edges between pairs of nodes. n aptures pairwise relationship between objects: Undirected graph represents
More information22 Elementary Graph Algorithms. There are two standard ways to represent a
VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph
More information22 Elementary Graph Algorithms. There are two standard ways to represent a
VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph
More information