Directed Acyclic Graphs && Topological Sorting

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

Topological Sort and Hashing
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
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.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Lecture 15 CSE 331 Oct 7, Mid-term stuff Chapters 1-3 in [KT] Sample mid-term (and graded HW3) at the END of class The web version has the correct.
All-Pairs Shortest Paths
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.
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.
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
CSC 331: Algorithm Analysis Decompositions of Graphs.
© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
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.
Topological Sort. Sorting technique over DAGs (Directed Acyclic Graphs) It creates a linear sequence (ordering) for the nodes such that: –If u has an.
Introduction to Algorithms
Topological Sorting.
Chapter 22 Elementary Graph Algorithms
Graphs-Part II Depth First Search (DFS)
CSE 373 Topological Sort Graph Traversals
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Chapter 3. Decompositions of Graphs
CSC 172 DATA STRUCTURES.
Basic Concepts Graphs For more notes and topics visit:
Finding a Path With Largest Smallest Edge
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.
Topological Sort (an application of DFS)
SINGLE-SOURCE SHORTEST PATHS IN DAGs
Chapter 5.
Directed Graphs Directed Graphs 1 Shortest Path Shortest Path
More Graph Algorithms.
Topological Sort.
Discussion section #2 HW1 questions?
Graph Algorithm.
Lecture 15 CSE 331 Sep 29, 2014.
Topological Sort.
CSC 172 DATA STRUCTURES.
Lecture 10 Algorithm Analysis
Graph Algorithm.
Graph Theory.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
"Learning how to learn is life's most important skill. " - Tony Buzan
Search Related Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Chapter 22: Elementary Graph Algorithms I
Graphs.
Lecture 13 Algorithm Analysis
Chapter 11 Graphs.
Lecture 19 CSE 331 Oct 13, 2010.
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 16 CSE 331 Oct 8, 2012.
Lecture 16 CSE 331 Oct 2, 2013.
Topological Sort (an application of DFS)
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
CSE 417: Algorithms and Computational Complexity
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Richard Anderson Lecture 5 Graph Theory
Graph Algorithm.
Assignment #2 (Assignment due: Nov. 06, 2018) v1 v2 v3 v4 v5
Topological Sorting Minimum Spanning Trees Shortest Path
Presentation transcript:

Directed Acyclic Graphs && Topological Sorting by Tian Cilliers IOI Training Camp 2 (3-4 February 2018)

Definitions Directed Acyclic Graph (DAG) A graph such that all of its edges are directed and there exist no cycles A DAG can be used to represent any transitive operation (that is, any operation ◦ where if a◦b and b◦c, then a◦c) Used in scheduling, version control, compilation dependencies, cryptocurrencies etc.

Definitions Topological Sort A sorting of the vertices of a DAG such that for directed edge uv from vertex u to vertex v, u comes before v in the ordering A graph is a DAG if and only if it is directed and has a topological sort (no cycles) There may be multiple existing topological orderings for any DAG A topological sorting can be easily reversed by reversing each edge

Algorithms Iterative Solution (Kahn’s Algorithm) Demonstration: 1 2 4 6 3 5

Algorithms Iterative Solution (Kahn’s Algorithm) Python Pseudocode:

Algorithms DFS Solution Demonstration: 1 2 4 6 3 5

Algorithms DFS Solution Pseudocode:

Algorithms Performance Both algorithms run in O(V+E) time, looping over every edge and every node exactly once Both algorithms require O(V+E) space, only differing by a constant DFS Solution might be more straightforward to implement, but requires a array keeping track of visited nodes as well as nodes in the current path, while Iterative Solution only needs to update in-degree of each node

Example Modified Alphabet (Codeforces Round 290 Div.1 Problem A) A list of names are written in lexicographical order, but not in a normal sense. Some modification to the order of the letters in the alphabet is needed so that the order of the names becomes lexicographical. Given a list of names, does there exist an order of letters in the Latin alphabet so that the names are following in lexicographical order? If so, you should find any such order. Sample IO: Input Output 3 rivest shamir adleman bcdefghijklmnopqrsatuvwxyz

Example Modified Alphabet (Codeforces Round 290 Div.1 Problem A) Solution: Between every consecutive pair of words, draw an edge between the first two different letters indicating that for the first word to be before the second one, the selected letter in the first word should come before the selected letter in the second word. A topological sorting of this graph gives the answer.

Example Substring (Codeforces Round 460 Div.2 Problem D) You are given a graph with n nodes and m directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "abaca", then the value of that path is 3. Your task is find a path whose value is the largest. Sample IO: Input Output 5 4 abaca 1 2 1 3 3 4 4 5 3

Example Substring (Codeforces Round 460 Div.2 Problem D) Solution: Use a DP approach, storing the amount of letters j you can get up to some node i in f[i][j]. Run a topological sorting algorithm (processing dependencies first) but for every node i, instead of adding any descendant k to a list containing the sort, update f[k][*] using f[i][*]

Questions?