Introduction to Graphs. CS2110, Spring 2011 Cornell University

Size: px
Start display at page:

Download "Introduction to Graphs. CS2110, Spring 2011 Cornell University"

Transcription

1 Introduction to Graphs CS2110, Spring 2011 Cornell University

2 A graph is a data structure for representing relationships.

3 Each graph is a set of nodes connected by edges.

4 Synonym Graph Hostile Slick Icy Chilly Direct Nifty Cool Abrupt Sharp Composed

5

6

7

8

9

10 Goals for Today Learn the formalisms behind graphs. Learn different representations for graphs. Learn about paths and cycles in graphs. See three ways of exploring a graph. Explore applications of graphs to real-world problems. Explore algorithms for drawing graphs.

11 Formalisms A (directed) graph is a pair G = (V, E) where V are the vertices (nodes) of the graph. E are the edges (arcs) of the graph. Each edge is a pair (u, v) of the start and end (or source and sink) of the edge.

12

13 CAN MAN RAN MAT CAT SAT RAT

14 Directed and Undirected Graphs A graph is directed if its edges specify which is the start and end node. Encodes asymmetric relationship. A graph is undirected if the edges don't distinguish between the start and end nodes. Encodes symmetric relationship. An undirected graph is a special case of a directed graph (just add edges both ways).

15 How Big is a Graph G = (V, E)? Two measures: Number of vertices: V (often denoted n) Number of edges: E (often denoted m) E can be at most O( V 2 ) A graph is called sparse if it has few edges. A graph with many edges is called dense.

16 Navigating a Graph B D A F C E

17 Navigating a Graph B D A F C E

18 Navigating a Graph B D A F A B D F C E

19 Navigating a Graph B D A F A C F C E

20 A path from v 0 to v n is a list of edges (v 0, v 1 ), (v 1, v 2 ),, (v n-1, v n ).

21 The length of a path is the number of edges it contains.

22 Navigating a Graph B D A F C E

23 Navigating a Graph B D A F C E

24 A node v is reachable from node u if there is a path from u to v.

25 Navigating a Graph B D A F C E

26 Navigating a Graph B D A F C E

27 Navigating a Graph B D A F B D B C E

28 Navigating a Graph B D A F B D B D B C E

29 Navigating a Graph B D A F C E

30 Navigating a Graph B D A F A B D B D F C E

31 A cycle in a graph is a set of edges (v 0, v 1 ), (v 1, v 2 ),, (v n, v 0 ) that starts and ends at the same node.

32 A simple path is a path that does not contain a cycle. A simple cycle is a cycle that does not contain a smaller cycle

33 Properties of Nodes B D A F C E

34 The indegree of a node is the number of edges entering that node. The outdegree of a node is the number of edges leaving that node. In an undirected graph, these are the same and are called the degree of the node.

35 Summary of Terminology A path is a series of edges connecting two nodes. The length of a path is the number of edges in the path. A node v is reachable from u if there is a path from u to v. A cycle is a path from a node to itself. A simple path is a path without a cycle. A simple cycle is a cycle that does not contain a nested cycle. The indegree and outdegree of a node are the number of edges entering/leaving it.

36 Representing Graphs

37 Adjacency Matrices n x n grid of boolean values. B D Element A ij is 1 if edge from i to j, 0 else. Memory usage is O(n2 ) A C E F Can check if an edge exists in O(1). Can find all edges entering or leaving a node in O(n). A B C D E F A B C D E F

38 Adjacency Lists List of edges leaving each node. A B D F Memory usage is O(m+n) C E Find edges leaving a node in O(d + (u)) Check if edge exists in O(d + (u)) A B C D B D E B C F F E F C E

39 Graph Algorithms

40 Representing Prerequisites Graph Path Cycle Degree Path Length Simple Path Reachability Simple Cycle

41 A directed acyclic graph (DAG) is a directed graph with no cycles.

42 Examples of DAGs

43 Examples of DAGs

44 Examples of DAGs

45 Traversing a DAG Graph Path Cycle Degree Path Length Simple Path Reachability Simple Cycle

46 Traversing a DAG Graph Path Cycle Degree Path Length Simple Path Reachability Simple Cycle

47 Traversing a DAG Graph Path Cycle Degree Path Length Simple Path Reachability Simple Cycle

48 Traversing a DAG Graph Path Cycle Degree Path Length Simple Path Reachability Simple Cycle

49 Traversing a DAG Graph Cycle Path Degree Path Length Simple Path Reachability Simple Cycle

50 Traversing a DAG Graph Cycle Path Degree Path Length Simple Path Reachability Simple Cycle

51 Traversing a DAG Graph Cycle Simple Cycle Path Degree Path Length Simple Path Reachability

52 Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

53 Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

54 Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

55 Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

56 Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Reachability Simple Path

57 Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

58 Traversing a DAG Graph Cycle Graph Simple Cycle Degree Path Cycle Degree Path Path Length Path Length Simple Path Reachability Simple Cycle Simple Path Reachability

59 Topological Sort Order the nodes of a DAG so no node is picked before its parents. Algorithm: Find a node with no incoming edges (indegree 0) Remove it from the graph. Add it to the resulting ordering. Not necessarily unique. Question: When is it unique?

60 Analyzing Topological Sort Assumes at each step that the DAG has a node with indegree zero. Is this always true? Claim one: Every DAG has such a node. Proof sketch: If this isn't true, then each node has at least one incoming edge. Start at any node and keep following backwards across that edge. Eventually you will find the same node twice and have found a cycle. Claim two: Removing such a node leaves the DAG a DAG. Proof sketch: If the resulting graph has a cycle, the old graph had a cycle as well.

61 Traversing an Arbitrary Graph

62 Traversing an Arbitrary Graph

63 Traversing an Arbitrary Graph

64 Traversing an Arbitrary Graph

65 Traversing an Arbitrary Graph

66 Traversing an Arbitrary Graph

67 Traversing an Arbitrary Graph

68 Traversing an Arbitrary Graph

69 Traversing an Arbitrary Graph

70 Traversing an Arbitrary Graph

71 Traversing an Arbitrary Graph

72 Traversing an Arbitrary Graph

73 Traversing an Arbitrary Graph

74 Traversing an Arbitrary Graph

75 Traversing an Arbitrary Graph

76 Traversing an Arbitrary Graph

77 Traversing an Arbitrary Graph

78 Traversing an Arbitrary Graph

79 Traversing an Arbitrary Graph

80 Traversing an Arbitrary Graph

81 Traversing an Arbitrary Graph

82 Traversing an Arbitrary Graph

83 Traversing an Arbitrary Graph

84 Traversing an Arbitrary Graph

85 Traversing an Arbitrary Graph

86 Traversing an Arbitrary Graph

87 Traversing an Arbitrary Graph

88 Traversing an Arbitrary Graph

89 Traversing an Arbitrary Graph

90 Traversing an Arbitrary Graph

91 Traversing an Arbitrary Graph

92 Traversing an Arbitrary Graph

93 Traversing an Arbitrary Graph

94 Traversing an Arbitrary Graph

95 General Graph Search Algorithm Maintain a collection C of nodes to visit. Initialize C with some set of nodes. While C is not empty: Pick a node v out of C. Follow all outgoing edges from v, adding each unvisited node found this way to C. Eventually explores all nodes reachable from the starting set of nodes. (Why?)

96 Depth-First Search Specialization of the general search algorithm where nodes to visit are put on a stack. Explores down a path as far as possible, then backs up. Simple graph search algorithm useful for exploring a complete graph. Useful as a subroutine in many important graph algorithms. Runs in O(m + n) with adjacency lists, O(n 2 ) with adjacency matrix.

97 Depth-first search A B C D E F

98 Depth-first search A B C D E F Stack

99 Depth-first search A B C D E F A Stack

100 Depth-first search A B C D E F Stack

101 Depth-first search A B C D E F E B Stack

102 Depth-first search A B C D E F E B Stack

103 Depth-first search A B C D E F B Stack

104 Depth-first search A B C D E F C F D B Stack

105 Depth-first search A B C D E F C F D B Stack

106 Depth-first search A B C D E F F D B Stack

107 Depth-first search A B C D E F F D B Stack

108 Depth-first search A B C D E F F D B Stack

109 Depth-first search A B C D E F D B Stack

110 Depth-first search A B C D E F D B Stack

111 Depth-first search A B C D E F B Stack

112 Depth-first search A B C D E F B Stack

113 Depth-first search A B C D E F Stack

114 Depth-first search A B C D E F Stack

115 Implementing DFS DFS(Node v, Set<Node> visited) { if (v is in visited) return; Add v to visited; } for (Node u connected to v) DFS(u, visited);

116 Graph Search Trees

117 Graph Search Trees

118 Graph Search Trees

119 Graph Search Trees

120 Graph Search Trees

121 Graph Search Trees

122 Graph Search Trees

123 Graph Search Trees

124 Graph Search Trees

125 Graph Search Trees

126 Graph Search Trees

127 Graph Search Trees

128 Graph Search Trees

129 Graph Search Trees

130 Graph Search Trees

131 Graph Search Trees

132 Graph Search Trees

133 Graph Search Trees

134 Graph Search Trees

135 Graph Search Trees

136 Graph Search Trees

137 Mazes as Graphs

138 Mazes as Graphs

139 Mazes as Graphs

140 Mazes as Graphs

141 Creating a Maze with DFS Create a grid graph of the appropriate size. Starting at any node, run a depth-first search, adding the arcs to the stack in random order. The resulting DFS tree is a maze with one solution.

142 Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes.

143 Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes. Stack

144 Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes. Stack

145 Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes. A C B Stack

146 Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes. A C B A B C Stack

147 Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes. A C B A B C Stack

148 Breadth-First Search Specialization of the general search algorithm where nodes to visit are put into a queue. Explores nodes one hop away, then two hops away, etc. Finds path with fewest edges from start node to all other nodes. Runs in O(m + n) with adjacency lists, O(n 2 ) with adjacency matrix.

149 Breadth-first search A B C D E F

150 Breadth-first search A B C D E F Queue

151 Breadth-first search A B C D E F Queue A

152 Breadth-first search A B C D E F Queue

153 Breadth-first search A B C D E F Queue B E

154 Breadth-first search A B C D E F Queue B E

155 Breadth-first search A B C D E F Queue E

156 Breadth-first search A B C D E F Queue E C

157 Breadth-first search A B C D E F Queue E C

158 Breadth-first search A B C D E F Queue C

159 Breadth-first search A B C D E F Queue C D F

160 Breadth-first search A B C D E F Queue C D F

161 Breadth-first search A B C D E F Queue D F

162 Breadth-first search A B C D E F Queue D F

163 Breadth-first search A B C D E F Queue F

164 Breadth-first search A B C D E F Queue F

165 Breadth-first search A B C D E F Queue

166 Breadth-first search A B C D E F Queue

167 Implementing BFS BFS(Node v, Set<Node> visited) { Create a Queue<Node> of nodes to visit; Add v to the queue; while (The queue is not empty) { Dequeue a node from the queue, let it be u; if (u has been visited) continue; Add u to the visited set; } } for (Node w connected to u) Enqueue w in the queue;

168 Classic Graph Algorithms

169 Graph Coloring Given a graph G, assign colors to the nodes so that no edge has endpoints of the same color. The chromatic number of a graph is the fewest number of colors needed to color it.

170 Graph Coloring is Hard. Determining whether a graph can be colored with k colors (for k > 2) is NP-complete. It is not known whether this problem can be solved in polynomial time. Want $1,000,000? Find a polynomial-time algorithm or prove that none exists.

171 Matching A matching in a graph is a subset of the edges that don't share any endpoints. Intuitively, pairing up nodes in the graph.

172 Matching A matching in a graph is a subset of the edges that don't share any endpoints. Intuitively, pairing up nodes in the graph.

173 Applications of Matching Unlike graph coloring, matching can be done quickly. Sample application: divvying up desserts.

174 Divvying Up Desserts

175 Divvying Up Desserts

176 Divvying Up Desserts

177 Divvying Up Desserts

178 Divvying Up Desserts

179 Divvying Up Desserts 3 7

180 Divvying Up Desserts

181 Divvying Up Desserts

182 Divvying Up Desserts

183 Divvying Up Desserts

184 Drawing Graphs

185 Hostile Slick Icy Chilly Direct Nifty Cool Abrupt Sharp Composed

186 Chilly Slick Composed Icy Direct Sharp Hostile Nifty Cool Abrupt

187

188

189

190

191 Idea: Treat the graph as a physical system that exerts forces on itself.

192 This is called a force-directed layout algorithm.

193 Summary Graphs are a powerful abstraction for modeling relationships and connectivity. Adjacency lists and adjacency matrices are two common representations of graphs. Directed acyclic graphs can be visited via a topological sort. Depth-first search is a simple graph exploration algorithm. Breadth-first search searches a graph one layer at a time. There are many classic algorithms on graphs: Graph coloring tries to color nodes so no two nodes of the same color are connected. Matchings represent pairing up of graph elements. Graph drawing seeks to render aesthetically-pleasing graphs.

GRAPHS Lecture 19 CS2110 Spring 2013

GRAPHS Lecture 19 CS2110 Spring 2013 GRAPHS Lecture 19 CS2110 Spring 2013 Announcements 2 Prelim 2: Two and a half weeks from now Tuesday, April16, 7:30-9pm, Statler Exam conflicts? We need to hear about them and can arrange a makeup It would

More information

GRAPHS Lecture 17 CS2110 Spring 2014

GRAPHS Lecture 17 CS2110 Spring 2014 GRAPHS Lecture 17 CS2110 Spring 2014 These are not Graphs 2...not the kind we mean, anyway These are Graphs 3 K 5 K 3,3 = Applications of Graphs 4 Communication networks The internet is a huge graph Routing

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 04 / 06 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Graphs Definition Terminology two ways to represent edges in implementation traversals

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

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

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes Graphs Graphs A graph is a data structure consisting of nodes (or vertices) and edges An edge is a connection between two nodes A D B E C Nodes: A, B, C, D, E Edges: (A, B), (A, D), (D, E), (E, C) Nodes

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

Lecture 9 Graph Traversal

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

CSE 100: GRAPH ALGORITHMS

CSE 100: GRAPH ALGORITHMS CSE 100: GRAPH ALGORITHMS 2 Graphs: Example A directed graph V5 V = { V = E = { E Path: 3 Graphs: Definitions A directed graph V5 V6 A graph G = (V,E) consists of a set of vertices V and a set of edges

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

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

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms CSE 373: Data Structures and Algorithms Lecture 15: Graph Data Structures, Topological Sort, and Traversals (DFS, BFS) Instructor: Lilian de Greef Quarter: Summer 2017 Today: Announcements Graph data structures

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

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

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

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

CS 491 CAP Introduction to Graphs and Search

CS 491 CAP Introduction to Graphs and Search CS 491 CAP Introduction to Graphs and Search Jingbo Shang University of Illinois at Urbana-Champaign Sep 9, 2016 Outline Graphs Adjacency Matrix vs. Adjacency List Special Graphs Depth-first and Breadth-first

More information

Lecture 22 Tuesday, April 10

Lecture 22 Tuesday, April 10 CIS 160 - Spring 2018 (instructor Val Tannen) Lecture 22 Tuesday, April 10 GRAPH THEORY Directed Graphs Directed graphs (a.k.a. digraphs) are an important mathematical modeling tool in Computer Science,

More information

Algorithms: Lecture 10. Chalmers University of Technology

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

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT PROJECT 3 500 Internal Error problems Hopefully all resolved (or close to) P3P1 grades are up (but muted) Leave canvas comment Emails tomorrow End of quarter GRAPHS

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

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

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

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq

More information

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

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

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description Plan CMPSCI 311: Introduction to Algorithms Akshay Krishnamurthy and Andrew McGregor University of Massachusetts Review: Breadth First Search Depth First Search Traversal Implementation and Running Time

More information

Fundamental Graph Algorithms Part Four

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

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation) MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review DFS, BFS Topological Sort MA/CSSE 473 Day 12 Questions? Interpolation Search Insertion sort analysis Depth first Search Breadth first

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

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

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

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs). Lecture 13 Graph Algorithms I 13.1 Overview This is the first of several lectures on graph algorithms. We will see how simple algorithms like depth-first-search can be used in clever ways (for a problem

More information

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013 CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals Ruth Anderson Winter 2013 Announcements Homework 4 due Friday Feb 15 th at the BEGINNING of lecture Project 2 Phase B due Tues

More information

Directed acyclic graphs

Directed acyclic graphs Directed acyclic graphs Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan March, 2 Directed Graphs 2 3 4 5 x v y Edges have direction. Cannot be traversed

More information

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering CS781 Lecture 2 January 13, 2010 Graph Traversals, Search, and Ordering Review of Lecture 1 Notions of Algorithm Scalability Worst-Case and Average-Case Analysis Asymptotic Growth Rates: Big-Oh Prototypical

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

1 Connected components in undirected graphs

1 Connected components in undirected graphs Lecture 10 Connected components of undirected and directed graphs Scribe: Luke Johnston (2016) and Mary Wootters (2017) Date: October 25, 2017 Much of the following notes were taken from Tim Roughgarden

More information

Reference Sheet for CO142.2 Discrete Mathematics II

Reference Sheet for CO142.2 Discrete Mathematics II Reference Sheet for CO14. Discrete Mathematics II Spring 017 1 Graphs Defintions 1. Graph: set of N nodes and A arcs such that each a A is associated with an unordered pair of nodes.. Simple graph: no

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

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction IS 709/809: Computational Methods in IS Research Graph Algorithms: Introduction Nirmalya Roy Department of Information Systems University of Maryland Baltimore County www.umbc.edu Motivation Several real-life

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

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions Introduction Chapter 9 Graph Algorithms graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 2 Definitions an undirected graph G = (V, E) is

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 3 Definitions an undirected graph G = (V, E)

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

Change in Schedule. I'm changing the schedule around a bit...

Change in Schedule. I'm changing the schedule around a bit... Graphs Announcements() { Change in Schedule I'm changing the schedule around a bit... Today: Graphs Tuesday: Shortest Path Algorithms Wednesday: Minimum Spanning Trees Tursday: Review Session for Midterm

More information

Algorithm Design and Analysis

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

Graphs - I CS 2110, Spring 2016

Graphs - I CS 2110, Spring 2016 Graphs - I CS 2110, Spring 2016 Announcements Reading: Chapter 28: Graphs Chapter 29: Graph Implementations These aren t the graphs we re interested in These aren t the graphs we re interested in This

More information

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 3 Graphs Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 3.1 Basic Definitions and Applications Undirected Graphs Undirected graph. G = (V, E) V = nodes. E

More information

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4. Topological Sort The goal of a topological sort is given a list of items with dependencies, (ie. item 5 must be completed before item 3, etc.) to produce an ordering of the items that satisfies the given

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

CSE 421 DFS / Topological Ordering

CSE 421 DFS / Topological Ordering CSE 421 DFS / Topological Ordering Shayan Oveis Gharan 1 Depth First Search Follow the first path you find as far as you can go; back up to last unexplored edge when you reach a dead end, then go as far

More information

Depth First Search (DFS)

Depth First Search (DFS) Algorithms & Models of Computation CS/ECE 374, Fall 2017 Depth First Search (DFS) Lecture 16 Thursday, October 26, 2017 Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 60 Today Two topics: Structure of directed

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

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1 Linear Time: O(n) CS 580: Algorithm Design and Analysis 2.4 A Survey of Common Running Times Merge. Combine two sorted lists A = a 1,a 2,,a n with B = b 1,b 2,,b n into sorted whole. Jeremiah Blocki Purdue

More information

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms Algorithms and Theory of Computation Lecture 3: Graph Algorithms Xiaohui Bei MAS 714 August 20, 2018 Nanyang Technological University MAS 714 August 20, 2018 1 / 18 Connectivity In a undirected graph G

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

Graphs & Digraphs Tuesday, November 06, 2007

Graphs & Digraphs Tuesday, November 06, 2007 Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements

More information

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies: UNIT 5 CSE 103 - Unit V- Graph GRAPH Graph is another important non-linear data structure. In tree Structure, there is a hierarchical relationship between, parent and children that is one-to-many relationship.

More information

Goals! CSE 417: Algorithms and Computational Complexity!

Goals! CSE 417: Algorithms and Computational Complexity! Goals! CSE : Algorithms and Computational Complexity! Graphs: defns, examples, utility, terminology! Representation: input, internal! Traversal: Breadth- & Depth-first search! Three Algorithms:!!Connected

More information

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications Chapter 3 31 Basic Definitions and Applications Graphs Slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley All rights reserved 1 Undirected Graphs Some Graph Applications Undirected graph G = (V,

More information

Graph Traversals. CS200 - Graphs 1

Graph Traversals. CS200 - Graphs 1 Graph Traversals CS200 - Graphs 1 Tree traversal reminder A Pre order A B D G H C E F I B C In order G D H B A E C F I D E F Post order G H D B E I F C A G H I Level order A B C D E F G H I Connected Components

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Graphs - Introduction Kostas Alexis Terminology In the context of our course, graphs represent relations among data items G = {V,E} A graph is a set of vertices

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

More Graph Algorithms

More Graph Algorithms More Graph Algorithms Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1 Announcements Calendar on the webpage has updated office hours for this week. Last Time: Dijkstra s Algorithm to find

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS CSE 101 Algorithm Design and Analysis Miles Jones mej016@eng.ucsd.edu Office 4208 CSE Building Lecture 5: SCC/BFS DECOMPOSITION There is a linear time algorithm that decomposes a directed graph into its

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

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

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Graphs: Introduction and First Algorithms Ulf Leser This Course Introduction 2 Abstract Data Types 1 Complexity analysis 1 Styles of algorithms 1 Lists, stacks, queues 2

More information

3.1 Basic Definitions and Applications

3.1 Basic Definitions and Applications Chapter 3 Graphs Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 3.1 Basic Definitions and Applications Undirected Graphs Undirected graph. G = (V, E) V = nodes. E

More information

CSE 417: Algorithms and Computational Complexity

CSE 417: Algorithms and Computational Complexity CSE : Algorithms and Computational Complexity More Graph Algorithms Autumn 00 Paul Beame Given: a directed acyclic graph (DAG) G=(V,E) Output: numbering of the vertices of G with distinct numbers from

More information

CS2 Algorithms and Data Structures Note 9

CS2 Algorithms and Data Structures Note 9 CS2 Algorithms and Data Structures Note 9 Graphs The remaining three lectures of the Algorithms and Data Structures thread will be devoted to graph algorithms. 9.1 Directed and Undirected Graphs A graph

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

Directed Acyclic Graphs & Topological Sort

Directed Acyclic Graphs & Topological Sort Directed Acyclic Graphs & Topological Sort CS6: Introduction to Data Structures & Algorithms Spring 08 Outline Directed Acyclic Graphs Topological Sort Hand-simulation Pseudo-code Runtime analysis 4 Directed

More information

Graphs. The ultimate data structure. graphs 1

Graphs. The ultimate data structure. graphs 1 Graphs The ultimate data structure graphs 1 Definition of graph Non-linear data structure consisting of nodes & links between them (like trees in this sense) Unlike trees, graph nodes may be completely

More information

Announcements. Assignment 6 due right now. Assignment 7 (FacePamphlet) out, due next Friday, March 16 at 3:15PM

Announcements. Assignment 6 due right now. Assignment 7 (FacePamphlet) out, due next Friday, March 16 at 3:15PM Graphs and Networks Announcements Assignment 6 due right now. Assignment 7 (FacePamphlet) out, due next Friday, March 6 at 3:5PM Build your own social network! No late days may be used. YEAH hours tonight

More information

Graphs and Graph Algorithms. Slides by Larry Ruzzo

Graphs and Graph Algorithms. Slides by Larry Ruzzo Graphs and Graph Algorithms Slides by Larry Ruzzo Goals Graphs: defns, examples, utility, terminology Representation: input, internal Traversal: Breadth- & Depth-first search Three Algorithms: Connected

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Introduction graph theory useful in practice represent many real-life problems can be if not careful with data structures Chapter 9 Graph s 2 Definitions Definitions an undirected graph is a finite set

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

Some major graph problems

Some major graph problems CS : Graphs and Blobs! Prof. Graeme Bailey http://cs.cs.cornell.edu (notes modified from Noah Snavely, Spring 009) Some major graph problems! Graph colouring Ensuring that radio stations don t clash! Graph

More information

Lecture 26: Graphs: Traversal (Part 1)

Lecture 26: Graphs: Traversal (Part 1) CS8 Integrated Introduction to Computer Science Fisler, Nelson Lecture 6: Graphs: Traversal (Part ) 0:00 AM, Apr, 08 Contents Introduction. Definitions........................................... Representations.......................................

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

Introduction to Algorithms

Introduction to Algorithms 6.006- Introduction to Algorithms Lecture 13 Prof. Constantinos Daskalakis CLRS 22.4-22.5 Graphs G=(V,E) V a set of vertices Usually number denoted by n E VxV a set of edges (pairs of vertices) Usually

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 04 / 13 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Graphs Depth First Search (DFS) Shortest path Shortest weight path (Dijkstra's

More information

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY General definitions; Representations; Graph Traversals; Topological sort; Graphs definitions & representations Graph theory is a fundamental tool in sparse

More information

Synonyms. Hostile. Chilly. Direct. Sharp

Synonyms. Hostile. Chilly. Direct. Sharp Graphs and Networks A Social Network Synonyms Hostile Slick Icy Direct Nifty Cool Abrupt Sharp Composed Chilly Chemical Bonds http://4.bp.blogspot.com/-xctbj8lkhqa/tjm0bonwbri/aaaaaaaaak4/-mhrbauohhg/s600/ethanol.gif

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

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

3.4 Testing Bipartiteness

3.4 Testing Bipartiteness 3.4 Testing Bipartiteness Bipartite Graphs Def. An undirected graph G = (V, E) is bipartite (2-colorable) if the nodes can be colored red or blue such that no edge has both ends the same color. Applications.

More information

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck Theory of Computing Lecture 4/5 MAS 714 Hartmut Klauck How fast can we sort? There are deterministic algorithms that sort in worst case time O(n log n) Do better algorithms exist? Example [Andersson et

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

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 3 Data Structures Graphs Traversals Strongly connected components Sofya Raskhodnikova L3.1 Measuring Running Time Focus on scalability: parameterize the running time

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

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

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

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be if not careful with data structures 3 Definitions an undirected graph G = (V, E) is a

More information

Synonyms. Hostile. Chilly. Direct. Sharp

Synonyms. Hostile. Chilly. Direct. Sharp Graphs and Networks A Social Network Synonyms Hostile Slick Icy Direct Nifty Cool Abrupt Sharp Composed Chilly Chemical Bonds http://4.bp.blogspot.com/-xctbj8lkhqa/tjm0bonwbri/aaaaaaaaak4/-mhrbauohhg/s600/ethanol.gif

More information