DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10, 2015 1.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

Topological Sort and Hashing
Graphs – Depth First Search ORD DFW SFO LAX
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
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.
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.
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.
Topics: 1. Finding a cycle in a graph 2. Propagation delay - example 3. Trees - properties מבנה המחשב - אביב 2004 תרגול 3#
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Lecture 10 Topics Application of DFS Topological Sort
Connected Components, Directed Graphs, Topological Sort COMP171.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
מבנה המחשב – מבוא למחשבים ספרתיים Foundations of Combinational Circuits תרגול מספר 3.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
1 Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithms. An Example. Implementation.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Graphs David Kauchak cs302 Spring DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch.
Topological Sort: Definition
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
Graphs Upon completion you will be able to:
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
SSSP in DAGs (directed acyclic graphs). DFS (depth first search) DFS(vertex v) { v.visited = TRUE; for each w adjacent to v do if(!w.visited) then dfs(w);
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
Week 11 - Wednesday.  What did we talk about last time?  Exam 2  And before that:  Graph representations  Depth first search.
Topological Sort. Sorting technique over DAGs (Directed Acyclic Graphs) It creates a linear sequence (ordering) for the nodes such that: –If u has an.
Topological Sorting.
Topological Sort In this topic, we will discuss: Motivations
CSE 2331/5331 Topic 9: Basic Graph Alg.
Topological Sort Minimum Spanning Tree
CISC 235: Topic 10 Graph Algorithms.
Depth-First Search.
SINGLE-SOURCE SHORTEST PATHS IN DAGs
Chapter 5.
CS120 Graphs.
More Graph Algorithms.
Topological Sort.
Topological Sort.
Paul Beame in lieu of Richard Anderson
"Learning how to learn is life's most important skill. " - Tony Buzan
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
Search Related Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Connected Components, Directed Graphs, Topological Sort
Directed Graph Algorithms
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
CSCI2100 Data Structures Tutorial
Lecture 16 CSE 331 Oct 8, 2012.
Lecture 16 CSE 331 Oct 2, 2013.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
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 .
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,

Outline 1) Directed Acyclic Graphs 2) Topological sort 1) Run-Through 2) Pseudocode 3) Runtime Analysis Tuesday, March 10,

Directed Acyclic Graphs (DAGs) A DAG is a graph that has two special properties: Directed: Each edge has an origin and a destination (visually represented by an arrow) directed not directed Acyclic: There is no way to start at a vertex and end up at the same vertex by traversing edges. There are no ‘cycles’ Which of these have cycles? Tuesday, March 10, B A B A A C D B A C D B A B yesnoyes

Directed Acyclic Graphs (DAGs) (2) DAGs are often used to model situations in which one element must come before another (course prerequisites, small tasks in a big project, etc.) Sources are vertices that have no incoming edges (no edges point to them) “socks” and “underwear” are sources Sinks are vertices that have no outgoing edges (no edges have that vertex as their origin) “shoes” and “belt” In-degree of a node is number of incoming edges Out-degree of a node is number of outgoing edges Tuesday, March 10, socks pants shoes belt Ex: Getting Dressed under- wear

33 Example DAG – Brown CS Course Prerequisites Sink Source Tuesday, March 10,

Intro to… Imagine that you are a CS concentrator trying to plan your courses for the next three years… How might you plan the order in which to take these courses? Topological sort! That’s how! Tuesday, March 10,

Topological Sort Topological ordering Ordering of vertices in a DAG For each vertex v, all of v’s “prerequisite” vertices are before v Topological sort Given a DAG, produce a topological ordering! If you lined up all vertices in topological order, all edges would point to the right One DAG can have multiple valid topological orderings Tuesday, March 10, Valid Topological Orderings: 1.15, 22, 16, , 16, 22, , 15, 16, 141 Sink Source

Top Sort: General Approach If a node is a source, there are no prerequisites, so we can visit it! Once we visit a node, we can delete all of its outgoing edges Deleting edges might create new sources, which we can now visit! Data Structures Needed: DAG we’re top-sorting Set of all sources (represented by a stack) List for our topological ordering Tuesday, March 10,

Topological Sort Run-Through Tuesday, March 10, List: Stack

Topological Sort Run-Through (2) Tuesday, March 10, Stack List: First: Populate Stack 15 22

Topological Sort Run-Through (3) Tuesday, March 10, Stack List: Next: Pop element from stack and add to list 15

Topological Sort Run-Through (4) Tuesday, March 10, Stack List: Next: Remove outgoing edges and check the corresponding vertices 15

Topological Sort Run-Through (5) Tuesday, March 10, Stack List: Next: Since 141 still has an incoming edge, move to next element on the stack 15 # incident edges: 1

Topological Sort Run-Through (6) Tuesday, March 10, Stack List: Next: Pop the next element off the stack and repeat this process until the stack is empty 15

Topological Sort Run-Through (7) Tuesday, March 10, Stack List: # incident edges: 0 This time, since 16 has an inDegree of 0, push it on the stack

Topological Sort Run-Through (8) Tuesday, March 10, Stack List: Rinse and repeat! 16

Topological Sort Run-Through (9) Tuesday, March 10, Stack List: # incident edges: 0 16

Stack Topological Sort Run-Through (10) Tuesday, March 10, List:

Topological Sort Run-Through (12) Tuesday, March 10, Stack List: # incident edges: 0

Topological Sort Run-Through (13) Tuesday, March 10, Stack List:

Topological Sort Run-Through (14) Tuesday, March 10, Stack List: # incident edges: 0

Topological Sort Run-Through (15) Tuesday, March 10, Stack List:

Topological Sort Run-Through (16) Tuesday, March 10, Stack List:

Topological Sort Run-Through (17) Tuesday, March 10, Stack List: # incident edges: 0

Topological Sort Run-Through (18) Tuesday, March 10, Stack List:

Topological Sort Run-Through (19) Tuesday, March 10, Stack List: All done!

Top Sort Pseudocode Tuesday, March 10, function topological_sort(G): //Input: A DAG G //Output: A list of the vertices of G in topological order S = Stack() L = List() for each vertex in G: if vertex has no incident edges: S.push(vertex) while S is not empty: v = S.pop() L.append(v) for each outgoing edge e from v: w = e.destination delete e if w has no incident edges: S.push(w) return L

Top Sort Runtime So, what’s the runtime? Let’s consider the major steps: 1. Create a set of all sources. 2. While the set isn’t empty, Remove a vertex from the set and add it to the sorted list For every edge from that vertex: Delete the edge from the graph Check all of its destination vertices and add them to the set if they have no incoming edges Tuesday, March 10,

Top Sort Runtime So, what’s the runtime? Let’s consider the major steps: 1. Create a set of all sources. 2. While the set isn’t empty, Remove a vertex from the set and add it to the sorted list For every edge from that vertex: Delete the edge from the graph Check all of its destination vertices and add them to the set if they have no incoming edges Tuesday, March 10, Step 1 requires looping through all of the vertices to find those with no incident edges – O(|V|)

Top Sort Runtime So, what’s the runtime? Let’s consider the major steps: 1. Create a set of all sources. 2. While the set isn’t empty, Remove a vertex from the set and add it to the sorted list For every edge from that vertex: Delete the edge from the graph Check all of its destination vertices and add them to the set if they have no incoming edges Tuesday, March 10, Step 2 also requires looping through every vertex, but also looks at edges. Since you only visit the edges that begin at that vertex, every edge gets visited only once. Thus, the runtime for this section is O(|V| + |E|). Step 1 requires looping through all of the vertices to find those with no incident edges – O(|V|)

31 Overall, this makes the algorithm run in O(|V|) + O(|V| + |E|) = O(2*|V| + |E|) = O(|V| + |E|) time. Top Sort Runtime So, what’s the runtime? Let’s consider the major steps: 1. Create a set of all sources. 2. While the set isn’t empty, Remove a vertex from the set and add it to the sorted list For every edge from that vertex: Delete the edge from the graph Check all of its destination vertices and add them to the set if they have no incoming edges Tuesday, March 10, Step 2 also requires looping through every vertex, but also looks at edges. Since you only visit the edges that begin at that vertex, every edge gets visited only once. Thus, the runtime for this section is O(|V| + |E|). Step 1 requires looping through all of the vertices to find those with no incident edges – O(|V|)

Top Sort Pseudocode – O(|V| + |E|) Tuesday, March 10, function topological_sort(G): //Input: A DAG G //Output: A list of the vertices of G in topological order S = Stack() L = List() for each vertex in G: O(|V|) if vertex has no incident edges: S.push(vertex) while S is not empty: O(|V| + |E|) v = S.pop() L.append(v) for each outgoing edge e from v: w = e.destination delete e if w has no incident edges: S.push(w) return L

Top Sort Variations What if we’re not allowed to remove edges from the input graph? That’s okay! Just use decorations. In the beginning: decorate each vertex with its in-degree Instead of removing an edge, just decrement the in- degree of the destination node. When the in-degree reaches 0, push it onto the stack! Tuesday, March 10,

Top Sort Variations (2) Do we need to use a stack in topological sort? Nope! Any data structure would do: queue, list, set, etc… Different data structures produce different valid orderings. But why do they all work? A node is only added to the data structure when it’s degree reaches 0 – i.e. when all of its “prerequisite” nodes have been processed and added to the final output list. This is an invariant throughout the course of the algorithm, so a valid topological order is always guaranteed! Tuesday, March 10,

Top Sort: Why only on DAGs? When is there no valid topological ordering? I need experience to get a job…I need a job to get experience…I need experience to get a job…I need a job to get experience…Uh oh! If there is a cycle there is no valid topological ordering! In fact, we can actually use topological sort to see if a graph contains a cycle If there are still edges left in the graph at the end of the algorithm, that means there must be a cycle. Tuesday, March 10, Job Experience