Algorithms and Data Structures

Slides:



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

CSE 373, Copyright S. Tanimoto, 2001 Graphs Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix,
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.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
Graphs – Depth First Search ORD DFW SFO LAX
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
1 Section 9.4 Spanning Trees. 2 Let G be a simple graph. A spanning subtree of G is a subgraph of G containing every vertex of G –must be connected; contains.
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?
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
CSC 331: Algorithm Analysis Decompositions of Graphs.
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Chapter 2 Graph Algorithms.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Topological Sorting.
Chapter 22 Elementary Graph Algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Lecture 12 Graph Algorithms
Lecture 10 Algorithm Analysis
Topological Sort CSE 373 Data Structures Lecture 19.
Graph Representation (23.1/22.1)
Graphs Part 2 Adjacency Matrix
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Richard Anderson Winter 2009 Lecture 6
Depth-First Search CSE 2011 Winter April 2019.
Data Structures and Algorithm Analysis Graph Algorithms
Richard Anderson Lecture 5 Graph Theory
Lecture 11 Graph Algorithms
Presentation transcript:

Algorithms and Data Structures Lecture 9

Agenda: Graph traverse Breadth-first search algorithm Depth-first search algorithm Topological sort

Graphs: traverse Sometimes we need to perform some procedure on all vertices of a graph Task can be accomplished visiting each vertex of a graph (from vertex from vertex) and performing required procedure Such task is known as graph traverse Let’s examine two well-known graph traverse algorithms: breadth-first search and depth-first search algorithms

Graphs: traverse The ideas behind graph traverse algorithms are the following: For given graph G and some vertex s, algorithm builds paths from vertex s to all vertices, attainable from s Paths built from a given vertex constitute a tree Some vertices of a graph (unattainable from vertex s) may remain out of a tree; procedure is repeated until all the vertices of a graph will be covered by some tree Trees (of a given graph) constitute a forest

Graphs: traverse

Graphs: traverse

Graphs: traverse

Graphs: breadth-first search Breadth-first search is one of graph traverse algorithms Algorithm is often used by other algorithms Algorithm is applicable for both directed and undirected graphs

Graphs: breadth-first search Input: graph G Output: breadth-first forest Any vertex of a G may be colored in white, gray or black color White vertices are unvisited, gray – discovered vertices, black – already processed vertices (finished) Q – auxiliary storage (queue) of FIFO type; holds discovered vertices (gray) Initially, G consists of white vertices only; finally, all the vertices of a G are black

Graphs: breadth-first search Step 1: all the vertices are marked as white Step 2: start with any white vertex s; it is marked as gray and added to the Q Step 3: get first vertex x from the Q Step 4: each adjacent to x white vertex is added to the Q and marked as gray Step 5: vertex x is removed from the Q, marked as black and “some” vertex processing procedure is performed (e.g. printing) Step 6: if Q is non-empty go to step 3 Step 7: continue from step 2 until G has no more white vertices

Graphs: breadth-first search, sample

Graphs: breadth-first search, sample

Graphs: breadth-first search, sample

Graphs: breadth-first search, sample

Graphs: breadth-first search, sample

Graphs: breadth-first search, sample

Graphs: breadth-first search, sample on usage

Graphs: breadth-first search, sample on usage

Graphs: breadth-first search, sample on usage

Graphs: breadth-first search, sample on usage

Graphs: breadth-first search For given graph G and some white vertex s, algorithm builds paths from vertex s to all vertices, attainable from s Paths are shortest Paths built from a given vertex constitute a breadth-first tree Breadth-first trees (constructed while considering distinct white vertices) constitute a breadth-first forest Changing order of considered white vertices may produce another breadth-first forest

Graphs: breadth-first search

Graphs: breadth-first search

Graphs: depth-first search Depth-first search is one of graph traverse algorithms Algorithm is often used by other algorithms Algorithm is applicable for both directed and undirected graphs

Graphs: depth-first search Input: graph G Output: depth-first forest Any vertex of a G may be colored in white, gray or black color White vertices are unvisited, gray – discovered vertices, black – already processed vertices (finished) Initially, G consists of white vertices only; finally, all the vertices of G are black

Graphs: depth-first search Step 1: all the vertices are marked as white Step 2: start with any white vertex s Step 3: vertex s is marked as gray Step 4: for each adjacent to s white vertex algorithm performs recursive reentrance (steps 3-5) Step 5: if there are no more adjacent to s white vertices in G, algorithm marks vertex s as black and performs some procedure under the vertex (e.g. prints data) Step 7: continue from step 2 until G has no more white vertices

Graphs: depth-first search, sample

Graphs: depth-first search, sample

Graphs: depth-first search, sample on usage

Graphs: depth-first search, sample on usage

Graphs: depth-first search, sample on usage

Graphs: depth-first search For given graph G and some white vertex s, algorithm builds paths from vertex s to all vertices, attainable from s Paths built from a given vertex constitute a depth-first tree Breadth-first trees (constructed while considering distinct white vertices) constitute a depth-first forest Changing order of considered white vertices may produce another depth-first forest

Graphs: depth-first search

Graphs: depth-first search

Graphs: topological sort Let G=<V,E> is a directed acyclic graph Let’s define comparison operation on V Vertices u and v are comparable and u<v if exists directed edge (u,v) Vertices u and w are comparable and u<w if exist some vertex v and edges (u,v) and (v,w); u<v & v<w => u<w Otherwise two vertices are incomparable Graph may not have cycles as relation u<u is invalid

Graphs: topological sort The task of topological sort assumes rearrangement of all graph vertices to a linear sequence Input: set of vertices V={v1,v2,…,vn} Output: sequence of vertices S=(v1,v2,…,vn), where vi<vk while i<k, or vi and vk are incomparable at all

Graphs: topological sort

Graphs: topological sort

Graphs: topological sort Algorithm: Step 1: Allocate storage S for sorted vertices, |S|=|V| Step 2: Perform Depth-First Search under the graph G; each finished (black) vertex is added to the end of the sequence S Step 3: Sequence S is topologically sorted set of vertices

Graphs: topological sort, sample

Graphs: topological sort, sample

Graphs: topological sort, sample

Graphs: topological sort, sample

Graphs: topological sort, sample

Graphs: topological sort, sample

Graphs: topological sort, sample on usage

Graphs: topological sort, sample on usage

Graphs: topological sort, sample on usage

Q & A