Chapter 22: Elementary Graph Algorithms I

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS
Chapter 9: Graphs Shortest Paths
Review Binary Search Trees Operations on Binary Search Tree
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Introduction to Graph  A graph consists of a set of vertices, and a set of edges that link together the vertices.  A graph can be: Directed: Edges are.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Graphs Data Structures and Algorithms A. G. Malamos Reference Algorithms, 2006, S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani Introduction to Algorithms,Third.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Introduction to Algorithms
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Data Structures and Algorithm Analysis Lecture 5
Graphs – Breadth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Lecture 12 Graph Algorithms
Introduction to Graphs
Ellen Walker CPSC 201 Data Structures Hiram College
CC 215 Data Structures Graph Searching
Unweighted Shortest Path Neil Tang 3/11/2010
CSCE 411 Design and Analysis of Algorithms
Graph.
CSE 421: Introduction to Algorithms
Elementary Graph Algorithms
Lecture 10 Algorithm Analysis
Graphs Representation, BFS, DFS
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Elementary graph algorithms Chapter 22
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Lectures on Graph Algorithms: searching, testing and sorting
Basic Graph Algorithms
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Lecture 13 CSE 331 Sep 27, 2017.
CSE 373 Data Structures Lecture 16
Breadth First Search - A B C D E F G H I front FIFO Queue.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Graphs Chapter 7 Visit for more Learning Resources.
Chapter 16 1 – Graphs Graph Categories Strong Components
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Elementary graph algorithms Chapter 22
3.2 Graph Traversal.
Elementary Graph Algorithms
Lecture 10 Graph Algorithms
Lecture 11 Graph Algorithms
Chapter 9: Graphs Shortest Paths
Lecture 13 CSE 331 Sep 28, 2016.
Presentation transcript:

Chapter 22: Elementary Graph Algorithms I

About this lecture Representation of Graph Adjacency List, Adjacency Matrix Breadth First Search

Graph 2 2 3 3 4 4 1 5 1 5 directed undirected

Adjacency List (1) For each vertex u, store its neighbors in a linked list 1 2 5 2 1 3 2 3 4 3 2 5 4 1 5 4 3 5 5 1 3 4 vertex neighbors

Adjacency List (2) For each vertex u, store its neighbors in a linked list 1 5 2 1 3 2 3 4 3 2 1 5 4 4 5 3 4 vertex neighbors

Adjacency List (3) Let G = (V, E) be an input graph Using Adjacency List representation : Space : O( |V| + |E| )  Excellent when |E| is small Easy to list all neighbors of a vertex Takes O(|V|) time to check if a vertex u is a neighbor of a vertex v can also represent weighted graph

Adjacency Matrix (1) Use a |V|  |V| matrix A such that A(u,v) = 1 if (u,v) is an edge A(u,v) = 0 otherwise 1 2 3 4 5 1 1 2 3 2 4 3 1 5 4 5

Adjacency Matrix (2) Use a |V|  |V| matrix A such that A(u,v) = 1 if (u,v) is an edge A(u,v) = 0 otherwise 1 2 3 4 5 1 1 2 3 2 4 3 1 5 4 5

Adjacency Matrix (3) Let G = (V, E) be an input graph Using Adjacency Matrix representation : Space : O( |V|2 )  Bad when |E| is small O(1) time to check if a vertex u is a neighbor of a vertex v (|V|) time to list all neighbors can also represent weighted graph

Transpose of a Matrix Let A be an n  m matrix Definition: The transpose of A, denoted by AT, is an m  n matrix such that AT(u,v) = A (v,u) for every u, v  If A is an adjacency matrix of an undirected graph, then A = AT

Breadth First Search (BFS) A simple algorithm to find all vertices reachable from a particular vertex s s is called source vertex Idea: Explore vertices in rounds At Round k, visit all vertices whose shortest distance (#edges) from s is k-1 Also, discover all vertices whose shortest distance from s is k

Stop if no vertices were discovered in Round k-1 The BFS Algorithm 1. Mark s as discovered in Round 0 2. For Round k = 1, 2, 3, …, For (each u discovered in Round k-1) { Mark u as visited ; Visit each neighbor v of u ; If (v not visited and not discovered) Mark v as discovered in Round k ; } Stop if no vertices were discovered in Round k-1

Example (s = source) r s t u r s t u v w x y v w x y r s t u visited ? 1 1 1 v w x y v w x y r s t u visited (? = discover time) ? 1 discovered (? = discover time) ? 1 1 direction of edge when new node is discovered v w x y

Example (s = source) r s t u r s t u 1 1 1 1 1 v w x y v w x y r s t u 1 2 1 2 1 1 1 2 v w x y v w x y r s t u visited (? = discover time) ? 1 2 discovered (? = discover time) ? 2 1 1 direction of edge when new node is discovered v w x y

Example (s = source) r s t u r s t u 1 2 1 2 3 2 1 1 1 1 2 v w x y v w 2 3 1 2 3 2 1 1 1 1 2 4 v w x y v w x y r s t u visited (? = discover time) ? 1 2 3 discovered (? = discover time) ? 4 2 1 1 direction of edge when new node is discovered v w x y

Done when no new node is discovered Example (s = source) r s t u 1 2 3 Done when no new node is discovered 2 1 1 4 v w x y r s t u 2 3 The directed edges form a tree that contains all nodes reachable from s Called BFS tree of s 1 2 1 1 4 v w x y

Correctness The correctness of BFS follows from the following theorem : Theorem: A vertex v is discovered in Round k if and only if shortest distance of v from source s is k Proof: By induction

Performance BFS algorithm is easily done if we use an O(|V|)-size array to store discovered/visited information a separate list for each round to store the vertices discovered in that round Since no vertex is discovered twice, and each edge is visited at most twice (why?)  Total time: O(|V|+|E|)  Total space: O(|V|+|E|)

Performance (2) Instead of using a separate list for each round, we can use a common queue When a vertex is discovered, we put it at the end of the queue To pick a vertex to visit in Step 2, we pick the one at the front of the queue Done when no vertex is in the queue No improvement in time/space … But algorithm is simplified

Homework Practice at home:1-2, 1-7, 2-4, 2-9

n-Queen Problem Write an algorithm that takes an integer n as input and determine the total number of solutions to the n-Queen problem (Bonus)