Graduate Algorithms CS F-15 Graphs, BFS, & DFS

Size: px
Start display at page:

Download "Graduate Algorithms CS F-15 Graphs, BFS, & DFS"

Transcription

1 Grauate Algorithms CS67-206F-5 Graphs, BFS, & DFS Davi Galles Department o Computer Science University o San Francisco

2 5-0: Graphs A graph consists o: A set o noes or vertices (terms are interchangeable) A set o eges or arcs (terms are interchangeable) Eges in graph can be either irecte or unirecte

3 5-: Graphs & Eges Eges can be labele or unlabele Ege labels are typically the cost associate with an ege e.g., Noes are cities, eges are roas between cities, ege label is the length o roa

4 5-2: Graph Representations Ajacency Matrix Represent a graph with a two-imensional array G G[i][j] = i there is an ege rom noe i to noe j G[i][j] = 0 i there is no ege rom noe i to noe j I graph is unirecte, matrix is symmetric Can represent eges labele with a cost as well: G[i][j] = cost o link between i an j I there is no irect link, G[i][j] =

5 5-: Ajacency Matrix Examples:

6 5-4: Ajacency Matrix Examples:

7 5-5: Ajacency Matrix Examples:

8 5-6: Ajacency Matrix Examples:

9 5-7: Graph Representations Ajacency List Maintain a linke-list o the neighbors o every vertex. n vertices Array o n lists, one per vertex Each list i contains a list o all vertices ajacent to i.

10 5-8: Ajacency List Examples: 0 2 2

11 5-9: Ajacency List Examples: Note lists are not always sorte

12 5-0: Sparse vs. Dense Sparse graph relatively ew eges Dense graph lots o eges Complete graph contains all possible eges These terms are uzzy. Sparse in one context may or may not be sparse in a ierent context

13 5-: Breath-First Search Metho or searching a graph Speciy a source noe in the grap Fin all noes reachable rom that noe First in all noes unit away Next in all noes 2 units away... etc

14 5-2: Breath-First Search Auxiliary Data Structures color or each vertex white, black, grey Use to make sure we on t visit vertices more than once Parent o each vertex (Path to source noe) Distance o each vertex rom source

15 5-: Breath-First Search BFS(G, s) or each vertex u in V[G] o color[u] WHITE [u] π[u] nil color[s] GRAY [s] 0 Q {s} while Q not empty o u Q.equeue or each v aj. to u i color[v] = WHITE color[v] GRAY [v] [u]+ π[v] u Q.enqueue(v) color[u] BLACK

16 5-4: Breath-First Search a b c e g h : b

17 5-5: Breath-First Search BFS computes the shortest path rom the start vertex to every other vertex We can run BFS on a irecte or unirecte tree Deines a BFS Tree Parent pointers p[v] BFS Tree is irecte

18 5-6: Breath-First Search a b c e g h :

19 5-7: Breath-First Search BFS Running time: V vertices E eges

20 5-8: Breath-First Search BFS Running time: V vertices E eges Running time Θ(V +E) In terms o just V, O(V 2 ) (why?)

21 5-9: Depth-First Search DFS(G) or each vertex v in G o color[v] WHITE π[v] = nil time 0 or each vertex v in G o i color[v] = WHITE DFS-VISIT(v)

22 5-20: Depth-First Search DFS-VISIT(v, G) color[v] GRAY time time + [v] time or each u ajacent to v in G o i color[u] = WHITE then π[u] v DFS-VISIT(u,G) color[v] BLACK time time + [v] time

23 5-2: Depth-First Search Do DFS, show iscover/inish times & Depth First Forst)

24 5-22: Depth-First Search DFS creates a Depth First Forest We can use DFS to classiy eges: Tree eges eges in the Depth First Forest Back Eges ege (u,v) that connects u to ancestor v in DFF Forwar eges non-tree ege (u,v) that connects u to escenent v in DFF Cross Eges Everything Else

25 5-2: Depth-First Search Labeling eges How coul we label eges (tree/back/orwar/cross) while we are oing DFS?

26 5-24: Depth-First Search Labeling eges How coul we label eges (tree/back/orwar/cross) while we are oing DFS? When examining ege (u,v), i v is: WHITE tree ege GRAY back ege BLACK orwar ege or cross ege

27 5-25: Depth-First Search Labeling eges Can we have cross eges in a DFS o an unirecte graph? Can we have orwar eges in a DFS o an unirecte graph?

28 5-26: Depth-First Search a b c e g Do DFS, show iscover/inish times & Depth First orest)

29 5-27: Depth-First Search a b c e g h i

30 5-28: Topological Sort Directe Acyclic Graph, Vertices v...v n Create an orering o the vertices I there a path rom v i to v j, then v i appears beore v j in the orering Example: Prerequisite chains

31 5-29: Topological Sort How coul we use DFS to o a Topological Sort? (Hint Use iscover an/or inish times)

32 5-0: Topological Sort How coul we use DFS to o a Topological Sort? (Hint Use iscover an/or inish times) (What oes it mean i noe x inishe beore noe y?)

33 5-: Topological Sort How coul we use DFS to o a Topological Sort? Do DFS, computing inishing times or each vertex As each vertex is inishe, a to ront o a linke list This list is a vali topological sort

34 5-2: Topological Sort Secon metho or oing topological sort: Which noe(s) coul be irst in the topological orering? Noe(s) with no incient (incoming) eges

35 5-: Topological Sort Pick a noe v k with no incient eges A v k to the orering Remove v k an all eges rom v k rom the graph Repeat until all noes are picke.

36 5-4: Topological Sort How can we in a noe with no incient eges? Count the incient eges o all noes

37 5-5: Topological Sort or (i=0; i < NumberOVertices; i++) NumIncient[i] = 0; or(i=0; i < NumberOVertices; i++) each noe k ajacent to i NumIncient[k]++

38 5-6: Topological Sort or(i=0; i < NumberOVertices; i++) NumIncient[i] = 0; or(i=0; i < NumberOVertices; i++) or(tmp=g[i]; tmp!= null; tmp=tmp.next()) NumIncient[tmp.neighbor()]++

39 5-7: Topological Sort Create NumIncient array Repeat Search through NumIncient to in a vertex v with NumIncient[v] == 0 A v to the orering Decrement NumIncient o all neighbors o v Set NumIncient[v] = - Until all vertices have been picke

40 5-8: Topological Sort In a graph with V vertices an E eges, how long oes this version o topological sort take?

41 5-9: Topological Sort In a graph with V vertices an E eges, how long oes this version o topological sort take? Θ(V 2 +E) = Θ(V 2 ) Since E O(V 2 )

42 5-40: Topological Sort Where are we spening extra time

43 5-4: Topological Sort Where are we spening extra time Searching through NumIncient each time looking or a vertex with no incient eges Keep aroun a set o all noes with no incient eges Remove an element v rom this set, an a it to the orering Decrement NumIncient or all neighbors o v I NumIncient[k] is ecremente to 0, a k to the set. How o we implement the set o noes with no incient eges?

44 5-42: Topological Sort Where are we spening extra time Searching through NumIncient each time looking or a vertex with no incient eges Keep aroun a set o all noes with no incient eges Remove an element v rom this set, an a it to the orering Decrement NumIncient or all neighbors o v I NumIncient[k] is ecremente to 0, a k to the set. How o we implement the set o noes with no incient eges? Use a stack

45 5-4: Topological Sort Examples!! Graph Ajacency List NumIncient Stack

46 5-44: More DFS Applications Depth First Search can be use to calculate the connecte components o a irecte graph First, some einitions an examples:

47 5-45: Strongly Connecte Graph Directe Path rom every noe to every other noe Strongly Connecte

48 5-46: Strongly Connecte Graph Directe Path rom every noe to every other noe Strongly Connecte

49 5-47: Connecte Components Subgraph (subset o the vertices) that is strongly connecte

50 5-48: Connecte Components Subgraph (subset o the vertices) that is strongly connecte

51 5-49: Connecte Components Subgraph (subset o the vertices) that is strongly connecte

52 5-50: Connecte Components Subgraph (subset o the vertices) that is strongly connecte

53 5-5: Connecte Components Connecte components o the graph are the largest possible strongly connecte subgraphs I we put each vertex in its own component each component woul be (trivially) strongly connecte Those woul not be the connecte components o the graph unless there were no larger connecte subgraphs

54 5-52: Connecte Components Calculating Connecte Components Two vertices v an v 2 are in the same connecte component i an only i: Directe path rom v to v 2 Directe path rom v 2 to v To in connecte components in irecte paths Use DFS: [v] an [v]

55 5-5: DFS Revisite Recall that we calculate the orer in which we visit the elements in a Depth-First Search For any vertex v in a DFS: [v] = Discovery time when the vertex is irst visite [v] = Finishing time when we have inishe with a vertex (an all o its chilren)

56 5-54: DFS Example

57 5-55: DFS Example

58 5-56: DFS Example

59 5-57: DFS Example

60 5-58: DFS Example

61 5-59: DFS Example

62 5-60: DFS Example

63 5-6: DFS Example

64 5-62: DFS Example

65 5-6: DFS Example

66 5-64: DFS Example

67 5-65: DFS Example

68 5-66: DFS Example

69 5-67: DFS Example

70 5-68: DFS Example

71 5-69: DFS Example

72 5-70: DFS Example

73 5-7: DFS Example

74 5-72: DFS Example

75 5-7: DFS Example

76 5-74: DFS Example

77 5-75: DFS Example

78 5-76: DFS Example

79 5-77: DFS Example

80 5-78: DFS Example

81 5-79: DFS Example

82 5-80: DFS Example

83 5-8: DFS Example

84 5-82: DFS Example

85 5-8: DFS Example

86 5-84: DFS Example

87 5-85: DFS Example

88 5-86: DFS Example

89 5-87: DFS Example

90 5-88: DFS Example

91 5-89: Using [] & [] Given two vertices v an v 2, what o we know i [v 2 ] < [v ]?

92 5-90: Using [] & [] Given two vertices v an v 2, what o we know i [v 2 ] < [v ]? Either: Path rom v to v 2 Start rom v Eventually visit v 2 Finish v 2 Finish v

93 5-9: Using [] & [] Given two vertices v an v 2, what o we know i [v 2 ] < [v ]? Either: Path rom v to v 2 No path rom v 2 to v Start rom v 2 Eventually inish v 2 Start rom v Eventually inish v

94 5-92: Using [] & [] I [v 2 ] < [v ]: Either a path rom v to v 2, or no path rom v 2 to v I there is a path rom v 2 to v, then there must be a path rom v to v 2 [v 2 ] < [v ] an a path rom v 2 to v v an v 2 are in the same connecte component

95 5-9: Calculating paths Path rom v 2 to v in G i an only i there is a path rom v to v 2 in G T G T is the transpose o G G with all eges reverse I ater DFS, [v 2 ] < [v ] Run secon DFS on G T, starting rom v, an v an v 2 are in the same DFS spanning tree v an v 2 must be in the same connecte component

96 5-94: Connecte Components Run DFS on G, calculating [] times Compute G T Run DFS on G T examining noes in inverse orer o inishing times rom irst DFS Any noes that are in the same DFS search tree in G T must be in the same connecte component

97 5-95: Connecte Components Eg

98 5-96: Connecte Components Eg

99 5-97: Connecte Components Eg

100 5-98: Connecte Components Eg

101 5-99: Connecte Components Eg

102 5-00: Connecte Components Eg

103 5-0: Connecte Components Eg

104 5-02: Connecte Components Eg

Data Structures and Algorithms

Data Structures and Algorithms Data Structures an Algorithms CS-0S-9 Connecte Components Davi Galles Department o Computer Science University o San Francisco 9-0: Strongly Connecte Graph Directe Path rom every noe to every other noe

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

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

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

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

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

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

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

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

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

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

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

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

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

Introduction to Algorithms A graph G =(V, E) V = set of vertices E = set of edges

Introduction to Algorithms A graph G =(V, E) V = set of vertices E = set of edges Introuction to Algorithms A graph G =(V, E) V = set of vertices E = set of eges In an unirecte graph: ege(u, v) = ege(v, u) Chapter 22: Elementary Graph Algorithms In a irecte graph: ege(u,v) goes 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

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

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

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

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

Graphs. 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 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 information

Additional Divide and Conquer Algorithms. Skipping from chapter 4: Quicksort Binary Search Binary Tree Traversal Matrix Multiplication

Additional Divide and Conquer Algorithms. Skipping from chapter 4: Quicksort Binary Search Binary Tree Traversal Matrix Multiplication Aitional Divie an Conquer Algorithms Skipping from chapter 4: Quicksort Binary Search Binary Tree Traversal Matrix Multiplication Divie an Conquer Closest Pair Let s revisit the closest pair problem. Last

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

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

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

3.1 Basic Definitions and Applications

3.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 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

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

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Practical 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 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

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

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

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

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

csci 210: Data Structures Graph Traversals

csci 210: Data Structures Graph Traversals csci 210: Data Structures Graph Traversals Graph traversal (BFS and DFS) G can be undirected or directed We think about coloring each vertex WHITE before we start GRAY after we visit a vertex but before

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

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

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

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

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

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Practical 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 (VG ) v1 v n V(G) = V - Set of all vertices in G E(G) = E - Set of all edges (u,v) in G,

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

Chapter 22 Elementary Graph Algorithms

Chapter 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 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

Jana 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 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 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

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

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

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

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

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

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

6.006 Introduction to Algorithms Spring 2008

6.006 Introduction to Algorithms Spring 2008 MIT OpenCourseWare http://ocw.mit.edu 6.006 Introduction to Algorithms Spring 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Introduction to Algorithms:

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

Outline. Graphs. Divide and Conquer.

Outline. Graphs. Divide and Conquer. GRAPHS COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges books. Outline Graphs.

More information

Basic Graph Algorithms (CLRS B.4-B.5, )

Basic Graph Algorithms (CLRS B.4-B.5, ) Basic Graph Algorithms (CLRS B.-B.,.-.) Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices V and a finite set of edges E. Directed graphs: E is a set of ordered pairs of vertices

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

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

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

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

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge Taking Stock IE170: in Systems Engineering: Lecture 19 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University March 16, 2007 Last Time Minimum This Time More Strongly Connected

More information

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Strongly 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 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

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

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

TIE Graph algorithms

TIE Graph algorithms TIE-20106 239 11 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

csci 210: Data Structures Graph Traversals

csci 210: Data Structures Graph Traversals csci 210: Data Structures Graph Traversals 1 Depth-first search (DFS) G can be directed or undirected DFS(v) mark v visited for all adjacent edges (v,w) of v do if w is not visited parent(w) = v (v,w)

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

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

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3 Multidimensional Arrays & Graphs CMSC 420: Lecture 3 Mini-Review Abstract Data Types: List Stack Queue Deque Dictionary Set Implementations: Linked Lists Circularly linked lists Doubly linked lists XOR

More information

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Undirected 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 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

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

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

Inf 2B: Graphs, BFS, DFS

Inf 2B: Graphs, BFS, DFS Inf 2B: Graphs, BFS, DFS Kyriakos Kalorkoti School of Informatics University of Edinburgh Directed and Undirected Graphs I A graph is a mathematical structure consisting of a set of vertices and a set

More information

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V. Directed and Undirected Graphs Inf 2B: Graphs, BFS, DFS Kyriakos Kalorkoti School of Informatics University of Edinburgh I A graph is a mathematical structure consisting of a set of vertices and a set

More information

Chapter 22: Elementary Graph Algorithms. Definitions:

Chapter 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 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

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

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

Quiz 2 Practice Problems

Quiz 2 Practice Problems Introduction to Algorithms: 6.006 Massachusetts Institute of Technology April 2, 2008 Professors Srini Devadas and Erik Demaine Handout 9 1 True/False Quiz 2 Practice Problems Decide whether these statements

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

Konigsberg Bridge Problem

Konigsberg 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

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS5-5S-6 Graph Traversals BFS & DFS David Galles Department of Computer Science University of San Francisco 6-: Graph Traversals Visit every vertex, in an order defined by

More information

Connectivity. Use DFS or BFS. 03/11/04 Lecture 18 1

Connectivity. Use DFS or BFS. 03/11/04 Lecture 18 1 Connectivity A (simple) undirected graph is connected if there exists a path between every pair of vertices. If a graph is not connected, then G (V,E ) is a connected component of the graph G(V,E) if V

More information

22.1 Representations of graphs

22.1 Representations of graphs 22.1 Representations of graphs There are two standard ways to represent a (directed or undirected) graph G = (V,E), where V is the set of vertices (or nodes) and E is the set of edges (or links). Adjacency

More information

Introduction to Graphs. CS2110, Spring 2011 Cornell University

Introduction to Graphs. CS2110, Spring 2011 Cornell University Introduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a set of nodes connected by edges. Synonym Graph Hostile Slick Icy

More information

Analysis of Algorithms Prof. Karen Daniels

Analysis of Algorithms Prof. Karen Daniels UMass Lowell omputer Science 91.404 nalysis of lgorithms Prof. Karen aniels Spring, 2013 hapter 22: raph lgorithms & rief Introduction to Shortest Paths [Source: ormen et al. textbook except where noted]

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

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

CS 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 information

Definition Example Solution

Definition Example Solution Section 11.4 Spanning Trees Definition: Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. Example: Find the spanning tree of this simple graph:

More information

Graphs: basic concepts and algorithms

Graphs: basic concepts and algorithms : basic concepts and algorithms Topics covered by this lecture: - Reminder Trees Trees (in-order,post-order,pre-order) s (BFS, DFS) Denitions: Reminder Directed graph (digraph): G = (V, E), V - vertex

More information

Solutions to Midterm 2 - Monday, July 11th, 2009

Solutions to Midterm 2 - Monday, July 11th, 2009 Solutions to Midterm - Monday, July 11th, 009 CPSC30, Summer009. Instructor: Dr. Lior Malka. (liorma@cs.ubc.ca) 1. Dynamic programming. Let A be a set of n integers A 1,..., A n such that 1 A i n for each

More information

UNIT IV -NON-LINEAR DATA STRUCTURES 4.1 Trees TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T1,

More information

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS473-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

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 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