CSE 2331/5331. Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort CSE 2331/5331

Similar documents
Graph Representation

Graph: representation and traversal

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

Design and Analysis of Algorithms

Review: Graph Theory and Representation

Data Structures and Algorithms. Chapter 7. Graphs

Trees. Arash Rafiey. 20 October, 2015

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Data Structures and Algorithms. Werner Nutt

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

22.3 Depth First Search

Elementary Graph Algorithms

Chapter 22 Elementary Graph Algorithms

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

Minimum Spanning Trees Ch 23 Traversing graphs

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

Graph: representation and traversal

Algorithm Design and Analysis

Lecture 9 Graph Traversal

Topic 12: Elementary Graph Algorithms

COT 6405 Introduction to Theory of Algorithms

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

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

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

Info 2950, Lecture 16

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

CS 310 Advanced Data Structures and Algorithms

Copyright 2000, Kevin Wayne 1

Representations of Graphs

Graph representation

Algorithm Design and Analysis

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

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

Graph Algorithms. Definition

Graph. Vertex. edge. Directed Graph. Undirected Graph

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

Goals! CSE 417: Algorithms and Computational Complexity!

Design and Analysis of Algorithms

Algorithm Design and Analysis

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

Algorithm Design and Analysis

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

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

Elementary Graph Algorithms

Chapter 22. Elementary Graph Algorithms

Lecture 3: Graphs and flows

Module 11: Additional Topics Graph Theory and Applications

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

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Unit 2: Algorithmic Graph Theory

Graph Representations and Traversal

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

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

3.1 Basic Definitions and Applications

Content. 1 Algorithms. 2 Assignment and Knapsack problem 3 Bin Packing. 4 Scheduling

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Lecture 5: Graphs & their Representation

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

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

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl

Graph Search Methods. Graph Search Methods

Solutions to relevant spring 2000 exam problems

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

CSE 100: GRAPH ALGORITHMS

GRAPHS Lecture 19 CS2110 Spring 2013

CS 206 Introduction to Computer Science II

Algorithm Design and Analysis

Graph Search Methods. Graph Search Methods

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

TIE Graph algorithms

Chapter 22: Elementary Graph Algorithms. Definitions:

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

Graph Algorithms Using Depth First Search

Algorithms: Lecture 10. Chalmers University of Technology

Graph Algorithms. Textbook reading. Chapter 3 Chapter 4. CSci 3110 Graph Algorithms 1/41

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Design and Analysis of Algorithms

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

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

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

Outline. Graphs. Divide and Conquer.

CSE 421 DFS / Topological Ordering

Unweighted directed graphs

GRAPHS Lecture 17 CS2110 Spring 2014

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

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Strongly Connected Components

CSE 417: Algorithms and Computational Complexity

Brute Force: Selection Sort

Figure 1: A directed graph.

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

CS/COE

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

22.1 Representations of graphs

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

Breadth First Search. cse2011 section 13.3 of textbook

Transcription:

Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort

What Is A Graph Graph G = (V, E) V: set of nodes E: set of edges a b c Example: d e f V ={ a, b, c, d, e, f } E ={(a, b), (a, d), (a, e), (b, c), (b, d), (b, e), (c, e), (e,f)}

Un-directed graph EE VV 2 Directed graph EE VV 2 a b c d e f

Un-directed graphs

Vertex Degree deg(v) = degree of v = # edges incident on v a b c d e f Lemma: vvii VV(GG) deg vv ii = 2 EE

Some Special Graphs Complete graph Path Cycle Planar graph Tree

Representations of Graphs Adjacency lists Each vertex u has a list, recording its neighbors i.e., all v s such that (u, v) E An array of V lists V[i].degree = size of adj list for node vv ii V[i].AdjList = adjacency list for node vv ii

Adjacency Lists For vertex v V, its adjacency list has size: deg(v) decide whether (v, u) E or not in time Θ (deg(v)) Size of data structure (space complexity): Θ( V + E ) = Θ (V+E)

Adjacency Matrix VV VV matrix AA AA ii, jj = 1 if vv ii, vv jj is an edge Otherwise, AA ii, jj = 0

Adjacency Matrix Size of data structure: Θ ( V V) Time to determine if (v, u) E : Θ(1) Though larger, it is simpler compared to adjacency list.

Sample Graph Algorithm Input: Graph G represented by adjacency lists Running time: Θ(V + E)

Connectivity A path in a graph is a sequence of vertices uu 1, uu 2,, uu kk such that there is an edge uu ii, uu ii+1 between any two adjacent vertices in the sequence Two vertices uu, ww VV GG are connected if there is a path in G from u to w. We also say that w is reachable from u. A graph G is connected if every pair of nodes uu, ww VV GG are connected.

Connectivity Checking How to check if the graph is connected? One approach: Graph traversal BFS: breadth-first search DFS: depth-first search

BFS: Breadth-first search Input: Given graph G = (V, E), and a source node s V Output: Will visit all nodes in V reachable from s For each vv VV, output a value vv. dd vv. dd = distance (smallest # of edges) from s to v. vv. dd = if v is not reachable from s.

Intuition Starting from source node s, Spread a wavefront to visit other nodes First visit all nodes one edge away from s Then all nodes two edges away from s Need a data-structure to store nodes to be explored.

Intuition cont. A node can be: un-discovered discovered, but not explored explored (finished) vv. dd : is set when node v is first discovered. Need a data structure to store discovered but unexplored nodes FIFO! Queue

Pseudo-code Use adjacency list representation Time complexity: Θ(V+E)

Correctness of Algorithm A node not reachable from s will not be visited A node reachable from s will be visited vv. dd computed is correct: Intuitively, if all nodes k distance away from s are in level k, and no other nodes are in level k, Then all nodes (k+1)-distance away from s must be in level (k+1). Rigorous proof by induction

BFS tree A node vv is the parent of uu if uu was first discovered when exploring vv A BFS tree TT Root: source node ss Nodes in level kk of TT are distance kk away from ss

Connectivity-Checking Time complexity: Θ(V+E)

Summary for BFS Starting from source node s, visits remaining nodes of graph from small distance to large distance This is one way to traverse an input graph With some special property where nodes are visited in non-decreasing distance to the source node s. Return distance between s to any reachable node in time Θ ( V + E )

DFS: Depth-First Search Another graph traversal algorithm BFS: Go as broad as possible in the algorithm DFS: Go as deep as possible in the algorithm

Example Perform DFS starting from vv 1 What if we add edge vv 1, vv 8

DFS Time complexity Θ(V+E)

Depth First Search Tree If v is discovered when exploring u Set vv. pppppppppppp = uu The collection of edges vv. pppppppppppp, vv form a tree, called Depth-first search tree.

DFS with DFS Tree

Example Perform DFS starting from vv 1

Another Connectivity Test

Traverse Entire Graph

Remarks DFS(G, k) Another way to compute all nodes reachable to the node vv kk Same time complexity as BFS There are nice properties of DFS and DFS tree that we are not reviewing in this class.

Directed Graphs

Un-directed graph EE VV 2 Directed graph Each edge uu, vv is directed from u to v EE VV 2 a b c d e f

Vertex Degree indeg(v) = # edges of the form uu, vv outdeg(v) = # edges of the form vv, uu a b c d e f Lemma: vvii VV(GG) indeg vv ii = EE Lemma: vvii VV(GG) outdeg vv ii = EE

Representations of Graphs Adjacency lists Each vertex u has a list, recording its neighbors i.e., all v s such that (u, v) E An array of V lists V[i].degree = size of adj list for node vv ii V[i].AdjList = adjacency list for node vv ii

Adjacency Lists For vertex v V, its adjacency list has size: outdeg(v) decide whether (v, u) E or not in time O(outdeg(v)) Size of data structure (space complexity): Θ(V+E)

Adjacency Matrix VV VV matrix AA AA ii, jj = 1 if vv ii, vv jj is an edge Otherwise, AA ii, jj = 0

Adjacency Matrix Size of data structure: Θ ( V V) Time to determine if (v, u) E : O(1) Though larger, it is simpler compared to adjacency list.

Sample Graph Algorithm Input: Directed graph G represented by adjacency list Running time: O(V + E)

Connectivity A path in a graph is a sequence of vertices uu 1, uu 2,, uu kk such that there is an edge uu ii, uu ii+1 between any two adjacent vertices in the sequence Given two vertices uu, ww VV GG, we say that w is reachable from u if there is a path in G from u to w. Note: w is reachable from u DOES NOT necessarily mean that u is reachable from w.

Reachability Test How many (or which) vertices are reachable from a source node, say vv 1?

BFS and DFS The algorithms for BFS and DFS remain the same Each edge is now understood as a directed edge BFS(V,E, s) : visits all nodes reachable from s in non-decreasing order

BFS Starting from source node s, Spread a wavefront to visit other nodes First visit all nodes one edge away from s Then all nodes two edges away from s

Pseudo-code Use adjacency list representation Time complexity: Θ(V+E)

Number of Reachable Nodes Compute # nodes reachable from vv kk Time complexity: Θ(V+E)

DFS: Depth-First Search Similarly, DFS remains the same Each edge is now a directed edge If we start with all nodes unvisited, Then DFS(G, kk) visits all nodes reachable to node vv kk BFS from previous NumReachable() procedure can be replaced with DFS.

More Example Is vv 1 reachable from vv 12? Is vv 12 reachable from vv 1?

DFS(G, k); DFS above can be replaced with BFS

Topological Sort

Directed Acyclic Graph A directed cycle is a sequence uu 1, uu 2,, uu kk, uu 1 such that there is a directed edge between any two consecutive nodes. DAG: directed acyclic graph Is a directed graph with no directed cycles.

Topological Sort A topological sort of a DAG G = (V, E) A linear ordering A of all vertices from V If edge (u,v) E => A[u] < A[v] undershorts pants socks shoes watch belt shirt tie jacket

Another Example Why requires DAG? Is the sorting order unique?

A topological sorted order of graph G exists if and only if G is a directed acyclic graph (DAG).

Question How to topologically sort a given DAG? Intuition: Which node can be the first node in the topological sort order? A node with in-degree 0! After we remove this, the process can be repeated.

Example undershorts pants socks shoes watch belt shirt tie jacket

Topological Sort Simplified Implementation

Time complexity Θ(V+E) Correctness: What if the algorithm terminates before we finish visiting all nodes? Procedure TopologicalSort(G) outputs a sorted list of all nodes if and only if the input graph G is a DAG If G is not DAG, the algorithm outputs only a partial list of vertices.

Remarks Other topological sort algorithm by using properties of DFS

Last Analyzing graph algorithms Adjacency list representation or Adjacency matrix representation CSE 2331 / 5331

Edge-weighted un-directed graph G = (V, E) and edge weight function ww: EE RR E.g, road network, where each node is a city, and each edge is a road, and weight is the length of this road. a 1 d 3 5 b 1 2 1 1 2 e 4 c f CSE 2331 / 5331

Example 1 Assume G is represented by adjacency list Q is priority-queue implemented by min-heap CSE 2331 / 5331

Example 2 Assume G is represented by adjacency matrix What if move line 10 to above line 8? What if move line 10 to above line 9? CSE 2331 / 5331