CSE 331: Introduction to Algorithm Analysis and Design Graphs

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

Strongly Connected Components

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

Problem Set 2 Solutions

Finding Strongly Connected Components

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

Kruskal s MST Algorithm

Graph Representation

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

Lecture 3: Graphs and flows

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Algorithm Design and Analysis

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 9 Notes. Class URL:

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

Lecture 10: Strongly Connected Components, Biconnected Graphs

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

6. Lecture notes on matroid intersection

Algorithms Activity 6: Applications of BFS

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

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

Design and Analysis of Algorithms

Copyright 2000, Kevin Wayne 1

Graph Definitions. In a directed graph the edges have directions (ordered pairs). A weighted graph includes a weight function.

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

Math 15 - Spring Homework 2.6 Solutions 1. (2.6 # 20) The following graph has 45 vertices. In Sagemath, we can define it like so:

Solutions to relevant spring 2000 exam problems

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

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

Matching Algorithms. Proof. If a bipartite graph has a perfect matching, then it is easy to see that the right hand side is a necessary condition.

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

7.3 Spanning trees Spanning trees [ ] 61

Lecture 19 Thursday, March 29. Examples of isomorphic, and non-isomorphic graphs will be given in class.

Computer Science 280 Fall 2002 Homework 10 Solutions

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

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

Algorithm Design and Analysis

CS4800: Algorithms & Data Jonathan Ullman

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

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

Shortest Path Problem

COL 702 : Assignment 1 solutions

Adjacent: Two distinct vertices u, v are adjacent if there is an edge with ends u, v. In this case we let uv denote such an edge.

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

Trees. Arash Rafiey. 20 October, 2015

Graph Algorithms. Definition

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico

Ma/CS 6b Class 5: Graph Connectivity

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Lecture 22 Tuesday, April 10

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

Lecture 20 : Trees DRAFT

Lecture 7. s.t. e = (u,v) E x u + x v 1 (2) v V x v 0 (3)

Question 2 (Strongly Connected Components, 15 points). What are the strongly connected components of the graph below?

Inf 2B: Graphs II - Applications of DFS

Notes 4 : Approximating Maximum Parsimony

Discrete Wiskunde II. Lecture 6: Planar Graphs

Minimum Spanning Trees Ch 23 Traversing graphs

Lecture 8: The Traveling Salesman Problem

CSE 421 Applications of DFS(?) Topological sort

Single Source Shortest Path

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

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

These notes present some properties of chordal graphs, a set of undirected graphs that are important for undirected graphical models.

Section 3.1: Nonseparable Graphs Cut vertex of a connected graph G: A vertex x G such that G x is not connected. Theorem 3.1, p. 57: Every connected

Notes for Recitation 8

1 Connected components in undirected graphs

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

1 Variations of the Traveling Salesman Problem

Design and Analysis of Algorithms

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

CS170 Discussion Section 4: 9/18

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

Solutions to In-Class Problems Week 4, Fri

Shortest path problems

Basic Graph Theory with Applications to Economics

Graph Algorithms Using Depth First Search

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

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

CS521 \ Notes for the Final Exam

2. CONNECTIVITY Connectivity

{ 1} Definitions. 10. Extremal graph theory. Problem definition Paths and cycles Complete subgraphs

Graphs and trees come up everywhere. We can view the internet as a graph (in many ways) Web search views web pages as a graph

CSE 421 DFS / Topological Ordering

Matching Theory. Figure 1: Is this graph bipartite?

22 Elementary Graph Algorithms. There are two standard ways to represent a

Chapter 22. Elementary Graph Algorithms

Greedy algorithms is another useful way for solving optimization problems.

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Goals! CSE 417: Algorithms and Computational Complexity!

Chapter 3: Paths and Cycles

Directed acyclic graphs

Graph Representations and Traversal

Number Theory and Graph Theory

Fundamental Graph Algorithms Part Three

Announcements. CSEP 521 Applied Algorithms. Announcements. Polynomial time efficiency. Definitions of efficiency 1/14/2013

Outline. Graphs. Divide and Conquer.

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

Transcription:

CSE 331: Introduction to Algorithm Analysis and Design Graphs 1 Graph Definitions Graph: A graph consists of a set of verticies V and a set of edges E such that: G = (V, E) V = {v 0, v 1,..., v n 1 } E = {e 0, e 1,..., e m 1 } V V e i = (u, v) where u V and v V. Note that V = n and E = m. Directed Graph: A graph is a directed graph if it can contain one-way edges without the same edge in the other direction. (u, w) E = (w, u) E Undirected Graph: A graph is an undirected graph if every edge in the graph has a counterpart between the same nodes in the opposite direction. (u, w) E = (w, u) E By definition, every undirected graph is also a directed graph. Unless otherwise stated, for this course you can assume that (u, u) / E and that graphs are undirected. Path: A path P in a graph is a sequence of nodes that are all connected by edges. P = (v 0, v 1,..., v k 1 ) v i V (v i, v i+1 ) E Length of a Path: The length of a path P is the number of edges in the path. For P = (v 0, v 1,..., v k 1 ) 1

the length of P is k 1. Cycle: A cycle is a path P the starts and ends at the same node. P = (v 0, v 1,..., v k 2, v 0 ) The minimum length of a cycle in an undirected graph is 3 (ie. no backtracking). The minimum length of a cycle in a directed graph is 2. Simple Path: A path P is simple if no nodes are repeated. This is equivalent to saying there are no cycles in P. For and v i, v j P i j = v i v j Connected: Nodes v i and v j are connected if there exists a path from one node to the other. In directed graphs, the nodes are strongly connected if there exists a path from v i and v j and a path from v j to v i. Connected Graph: A graph is connected if v i and v j are connected for all nodes v i, v j V. Distance: The distance between connected nodes v i and v j connecting v i and v j. is the length of the shortest path Tree: A tree is a connected graph that contains no cycles. Forest: A graph is a forest if it has no cycles. A forest can be thought of as a set of trees. By definition, every tree is a forest. 2 Tree Proof For an undirected graph G, any 2 of the following conditions implies the third. 1. G is connected 2. G has no cycles 3. G has n 1 edges In this section, we will prove that properties 1 and 2 imply property 3. This is equivalent to showing that every tree T = (V, E) has exactly n 1 edges. Theorem 1. Every undirected tree T = (V, E) has exactly n 1 edges. Proof. We first setup the proof by adding some helpful descriptions of T without altering its structure. We first pick an arbitrary node r V to be the root of T. We then orient each edge in T towards r (ie. each edge points up to the previous layer in the tree). The graph is undirected, but we describing it as directed to make the proof easier to describe. 2

Claim 1: Every non-root vertex u has exactly 1 outgoing edge. We will prove this claim using contradiction by assuming that there exists a node u that doesn t have 1 outgoing edge. We will break this into 2 cases. Case 1: If u has 0 outgoing edges, then u is not connected to the rest of T. Specifically, since we direct each edge towards r, the lack of an edge implies that there is no path from u to r. This contradicts the condition that T is connected. Case 2: If u has 2 or more outgoing edges, then there is a cycle in T. Since the edges are directed towards r, we know that each outgoing edge leads to a node that is connected to r implying that the two nodes are connected. This creates a cycle by taking this path connecting the two node through r, then connecting the two nodes again through the two outgoing edges from u 1. This contradicts the condition that T has no cycles. Since both of these cases leads to a contradiction, every non-root node must have exactly 1 outgoing edge. Claim 2: The root r has 0 outgoing edges. Since the edges are directed towards r, there are no outgoing edges from r. Claim 3: Every edge is an outgoing edge for exactly 1 non-root vertex. This follows from the definition of an edge. An edge has exactly 1 source node and 1 termination node. By combining claims 1 and 3 above, we can see that there is 1-1 correspondence between edges and non-root verticies. This follows since each non-root vertex has 1 outgoing edge and each edge is outgoing from 1 non-root vertex. There is one root with no outgoing edges so there are n 1 edges in T. 3 BFS Levels Theorem 2. If T is a BFS tree for G = (V, E) and x L i, y L j, and (x, y) E, then i and j differ by at most 1 ( i j 1). Proof. Without loss of generality (wlog), i j. In other words, label the lesser of the two levels i. Since i and j are just labels, we can do this without losing the generality of the proof. This can be thought of as a shortcut which is equivalent to writing the same proof twice with i and j reversed. Instead, we relabel them and write the proof only once in terms of i being the smaller value (or equal). This will be a proof by contradiction. We will assume that i j (or i < j 1) and show that this leads to a contradiction. By the BFS algorithm we know that x L i and that when exploring L i+1, edge (x, y) must be taken as no edges connecting the explored nodes to the unexplored nodes can be skipped. Since i j, we know the algorithm explores x before, or at the same time, as y. By our assumption of i < j 1, we know that j > i+1 meaning that y is at least 2 levels below x. This implies that the algorithm skipped the edge (x, y) when exploring L i+1 which is a contradiction. proof 1 Recall that this is an undirected graph and the direction of the edges is only used for describing them in the 3

Theorem 3. If T is a BFS tree for G = (V, E) rooted at node s and x L i then the shortest path from s to x contains i edges. Proof. Proof by induction on the number of edges in the path i. Base case: For i = 0, the only node is the root s which needs no edges to get to itself. Induction step: Assume that the shortest path from s to any node u L i 1 contains i 1 edges and prove that the shortest path from s to any node v L i contains i edges. To prove this, we note that any such v must have an edge connecting it to a node v L i 1 in order for it to be explored by the BFS algorithm. By the inductive hypothesis, we know that the shortest path from s to v contains i 1 edges. By adding the edge (v, v) we have a path from s to v with i edges as desired. To finish the proof, we note that if there were a shorter path from s to v, then v would have been explored in a previous level. This is true by applying Theorem 2 to the shortest path from s to v. At each step along the path, the level increases by at most 1 which implies the shortest path contains at least i edges. 4 Connected Component Proof We will now prove that the following explore(s) algorithm computes the connected component of a node s in a graph G = (V, E). explore(s) : 1. R = {s} 2. while (u,w) E (u R w / R) (a) R = R {w} 3. return R This algorithm starts at s, as explores all nodes that are reachable from an already explored node. Theorem 4. The output R of explore(s) is the connected component of s, cc(s). Proof. For this theorem, we have to prove that R = cc(s), both of which are sets. To prove set equality, we usually divide it into two separate proofs being R cc(s) and cc(s) R. If both of these are true, then R = cc(s) similar to x y and y x implying that x = y. Once we prove these two subset properties, we will have proven the theorem. R cc(s): To prove this inequality, we will prove w R = w cc(s) (every element in R is also in cc(s)). This is true by observing that an edge w is only added to R if there exist a path from s to w since the algorithm only explores a node by traveling along edges. Since there is a path from s to w, they are connected. 4

cc(s) R: Now we must prove w cc(s) = w R which is the more difficult direction of this proof 2. This will be a proof by contradiction of implication so we will assume that there is a node w in cc(s) that is not in R (w cc(s) and w / R). Since w / R, we know that w G \ R. Also, since w cc(s), there must exist a path p from s to w. Then there must exists an edge (x, y) along p that connects R to G \ R such that x R and y / R which we call a crossing edge between the two subgraphs 3. However, in explore, this edge must be explored in the while loop before termination which a contradiction of the algorithm. We have proven that R cc(s) and cc(s) R which implies that R = cc(s). Thus, explore(s) outputs R which is the connected component of s. 5 Topological Ordering Proof Define a topological ordering to be and ordering of all the nodes in a graph u 0, u 1,..., u n 1 such that (u i, u j ) E = i < j. Theorem 5. If G has a topological ordering, then G is a directed acyclic graph (DAG). Proof. Proof by contradiction: Assume G is not a DAG, G has a directed cycle c. Let u i be the node in c with the smallest index i (ie. u i is the earliest node in the topological ordering of G. Then there must exists some edge (u j, u i ) c such that j > i which is a contradiction of G having a topological ordering. Therefor, G must have no cycles and is a DAG. 2 When proving set equality, it is common for one of the inequalities to much simpler to prove than the other 3 It s possible that x = s and/or y = w 5