CSE 373 Topological Sort Graph Traversals

Slides:



Advertisements
Similar presentations
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
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?
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.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
CSE 373: Data Structures & Algorithms Lecture 17: Topological Sort / Graph Traversals Linda Shapiro Winter 2015.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Introduction to Algorithms
Graphs Chapter 20.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Graphs Representation, BFS, DFS
Data Structures and Algorithms
Data Structures, Algorithms & Complexity
Lecture 11 Graph Algorithms
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
CSE 2331/5331 Topic 9: Basic Graph Alg.
Cse 373 May 8th – Dijkstras.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
C.Eng 213 Data Structures Graphs Fall Section 3.
Depth-First Search.
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Catie Baker Spring 2015.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Instructor: Lilian de Greef Quarter: Summer 2017
CS120 Graphs.
CSE 373: Data Structures & Algorithms Lecture 17: Topological Sort / Graph Traversals Linda Shapiro Spring 2016.
Graph.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Graph Algorithm.
Lecture 10 Algorithm Analysis
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Paul Beame in lieu of Richard Anderson
Search Related Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Graph Representation (23.1/22.1)
Chapter 11 Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Connected Components, Directed Graphs, Topological Sort
Lecture 13 CSE 331 Sep 27, 2017.
Directed Graph Algorithms
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
CE 221 Data Structures and Algorithms
GRAPHS G=<V,E> Adjacent vertices Undirected graph
CE 221 Data Structures and Algorithms
Important Problem Types and Fundamental Data Structures
Lecture 10 Graph Algorithms
Lecture 13 CSE 331 Sep 28, 2016.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

CSE 373 Topological Sort Graph Traversals Spring 2015 Yanling He

Graphs A Graph G = (V, E) Complexity is O(|E|+|V|) is O(|V|^2) Represents relationships among items Can be directed or undirected Complexity is O(|E|+|V|) is O(|V|^2)

Graph Data Structure Adjacency Matrix Adjacency List Assign each node a number from 0 to |V|-1 A |V| x |V| matrix of booleans If M is the matrix, then M[u][v] being true means there is an edge from u to v Adjacency List An array of length |V| in which each entry stored a list of all adjacent vertices

Topological Sort Given a DAG, output all vertices in an order so that no vertex appears before another vertex that points to it

Topological Sort Why do we perform topological sorts only on DAGs? Is there always a unique answer? Do some DAGs have exactly 1 answer?

Topological Sort Why do we perform topological sorts only on DAGs? Because a cycle means there is no correct answer Is there always a unique answer? No, there can be 1 or more answers; depends on the graph Do some DAGs have exactly 1 answer? Yes, including all lists

Topological Sort Algorithm Keep track of the in-degree of each node Use a queue to ensure the proper ordering of nodes (from least to greatest in-degree) Every time an in-degree is 0, enqueue it. Every time a node is processed, decrement it’s adjacents’ in-degree.

Topological Sort Example

Topological Sort A B C D E Queue Contents Step 1 2 1 (A, B) Step 2 (B) 2 1 (A, B) Step 2 (B) Step 3 (C, D) Step 4 (D, E) Step 5 (E) Step 6 ()

Topological Sort Running Time Initialization: O(|V|+|E|) (assuming adjacency list) Sum of all enqueues and dequeues: O(|V|) Sum of all decrements: O(|E|) (assuming adjacency list) So total is O(|E|+|V|) – much better for sparse graphs

Graph Traversals Depth-First Search Breadth-First Search Recursively explore one part before going back to the other parts not yet explored Typically use a stack to keep track of which nodes to process next (non-recursive) Breadth-First Search Explore areas closer to the start node first Typically use a queue to keep track of which nodes to process next

Graph Traversals Depth First Search

Graph Traversals Depth First Search

Graph Traversals Breadth First Search

Graph Traversals Comparison Breath first finds shortest paths Better for “what is the shortest path from x to y” But depth-first can use less space in finding a path