2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs.

Slides:



Advertisements
Similar presentations
CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort.
Advertisements

Introduction to Algorithms Quicksort
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
David Luebke 1 5/20/2015 CS 332: Algorithms Quicksort.
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Introduction to Algorithms Chapter 7: Quick Sort.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
7.Quicksort Hsu, Lih-Hsing. Computer Theory Lab. Chapter 7P Description of quicksort Divide Conquer Combine.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Graph & BFS.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS421 - Course Information Website Syllabus Schedule The Book:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Connected Components, Directed Graphs, Topological Sort COMP171.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Spring 2015 Lecture 10: Elementary Graph Algorithms
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Introduction to Algorithms Jiafen Liu Sept
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
Deterministic and Randomized Quicksort Andreas Klappenecker.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
CSE 340: Review (at last!) Measuring The Complexity Complexity is a function of the size of the input O() Ω() Θ() Complexity Analysis “same order” Order.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
CSC317 1 Hiring problem-review Cost to interview (low C i ) Cost to fire/hire … (expensive C h ) n number of candidates m hired O (c i n + c h m) Independent.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677
Topological Sorting.
Quick Sort Divide: Partition the array into two sub-arrays
CSE 2331/5331 Topic 9: Basic Graph Alg.
CSC 413/513: Intro to Algorithms
CS 3343: Analysis of Algorithms
Ch 7: Quicksort Ming-Te Chi
Lecture 3 / 4 Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS 583 Analysis of Algorithms
Design and Analysis of Algorithms
CS 332: Algorithms Quicksort David Luebke /9/2019.
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Quicksort Quick sort Correctness of partition - loop invariant
Presentation transcript:

2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs

Sorting algorithms Input: a sequence of n numbers ‹a 1, a 2, …, a n › Output: a permutation of the input such that ‹ a i1 ≤ … ≤ a in › Important properties of sorting algorithms: running time: how fast is the algorithm in the worst case in place: only a constant number of input elements are ever stored outside the input array

Sorting algorithms Input: a sequence of n numbers ‹a 1, a 2, …, a n › Output: a permutation of the input such that ‹ a i1 ≤ … ≤ a in › Important properties of sorting algorithms: running time: how fast is the algorithm in the worst case in place: only a constant number of input elements are ever stored outside the input array Θ(n 2 ) yes Θ(n log n)no worst case running timein place Selection Sort Insertion Sort Merge Sort Θ(n 2 ) yes

QuickSort One more sorting algorithm …

worst case running timein place Selection Sort Θ(n 2 ) yes Insertion Sort Θ(n 2 ) yes Merge Sort Θ(n log n) no Quick Sort Θ(n 2 ) yes QuickSort Why QuickSort? 1.Expected running time: Θ(n log n) (randomized QuickSort) 2.Constants hidden in Θ(n log n) are small 3.using linear time median finding to guarantee good pivot gives worst case Θ(n log n)

QuickSort  QuickSort is a divide-and-conquer algorithm To sort the subarray A[p..r]: Divide Partition A[p..r] into two subarrays A[p..q-1] and A[q+1..r], such that each element in A[p..q-1] is ≤ A[q] and A[q] is < each element in A[q+1..r]. Conquer Sort the two subarrays by recursive calls to QuickSort Combine No work is needed to combine the subarrays, since they are sorted in place.  Divide using a procedure Partition which returns q.

QuickSort QuickSort(A, p, r) 1. if p < r 2. then q = Partition(A, p, r) 3. QuickSort(A, p, q-1) 4. QuickSort(A, q+1, r) Partition(A, p, r) 1. x = A[r] 2. i = p-1 3. for j = p to r-1 4. do if A[j] ≤ x 5. then i = i+1 6. exchange A[i] ↔ A[j] 7. exchange A[i+1] ↔ A[r] 8. return i+1  Initial call: QuickSort(A, 1, n)  Partition always selects A[r] as the pivot (the element around which to partition)

Partition  As Partition executes, the array is partitioned into four regions (some may be empty) Loop invariant 1.all entries in A[p..i] are ≤ pivot 2.all entries in A[i+1..j-1] are > pivot 3.A[r] = pivot Partition(A, p, r) 1. x = A[r] 2. i = p-1 3. for j = p to r-1 4. do if A[j] ≤ x 5. then i = i+1 6. exchange A[i] ↔ A[j] 7. exchange A[i+1] ↔ A[r] 8. return i+1 x pijr ≤ x≤ x> x???

Partition x p ij r ≤ x≤ x> x ??? Partition(A, p, r) 1. x = A[r] 2. i = p-1 3. for j = p to r-1 4. do if A[j] ≤ x 5. then i = i+1 6. exchange A[i] ↔ A[j] 7. exchange A[i+1] ↔ A[r] 8. return i p ij r p ij r p ij r p ij r p ij r p ij r p ij r p i r p i r

Loop invariant 1.all entries in A[p..i] are ≤ pivot 2.all entries in A[i+1..j-1] are > pivot 3.A[r] = pivot Initialization before the loop starts, all conditions are satisfied, since r is the pivot and the two subarrays A[p..i] and A[i+1..j-1] are empty Maintenance while the loop is running, if A[j] ≤ pivot, then A[j] and A[i+1] are swapped and then i and j are incremented ➨ 1. and 2. hold. If A[j] > pivot, then increment only j ➨ 1. and 2. hold. Partition - Correctness Partition(A, p, r) 1. x = A[r] 2. i = p-1 3. for j = p to r-1 4. do if A[j] ≤ x 5. then i = i+1 6. exchange A[i] ↔ A[j] 7. exchange A[i+1] ↔ A[r] 8. return i+1 x pij ≤ x≤ x> x??? r

Loop invariant 1.all entries in A[p..i] are ≤ pivot 2.all entries in A[i+1..j-1] are > pivot 3.A[r] = pivot Termination when the loop terminates, j = r, so all elements in A are partitioned into one of three cases: A[p..i] ≤ pivot, A[i+1..r-1] > pivot, and A[r] = pivot  Lines 7 and 8 move the pivot between the two subarrays Running time: Partition - Correctness Partition(A, p, r) 1. x = A[r] 2. i = p-1 3. for j = p to r-1 4. do if A[j] ≤ x 5. then i = i+1 6. exchange A[i] ↔ A[j] 7. exchange A[i+1] ↔ A[r] 8. return i+1 x pij ≤ x≤ x> x??? r Θ(n) for an n-element subarray

QuickSort: running time QuickSort(A, p, r) 1. if p < r 2. then q = Partition(A, p, r) 3. QuickSort(A, p, q-1) 4. QuickSort(A, q+1, r)  Running time depends on partitioning of subarrays: if they are balanced, then QuickSort is as fast as MergeSort if they are unbalanced, then QuickSort can be as slow as InsertionSort Worst case subarrays completely unbalanced: 0 elements in one, n-1 in the other T(n) = T(n-1) + T(0) + Θ(n) = T(n-1) + Θ(n) = Θ(n 2 ) input: sorted array

QuickSort: running time QuickSort(A, p, r) 1. if p < r 2. then q = Partition(A, p, r) 3. QuickSort(A, p, q-1) 4. QuickSort(A, q+1, r)  Running time depends on partitioning of subarrays: if they are balanced, then QuickSort is as fast as MergeSort if they are unbalanced, then QuickSort can be as slow as InsertionSort Best case subarrays completely balanced: each has ≤ n/2 elements T(n) = 2T(n/2) + Θ(n) = Θ(n log n) Average? Much closer to best case than worst case …

Randomized QuickSort  pick pivot at random RandomizedPartition(A, p, r) 1. i = Random(p, r) 2. exchange A[r] ↔A[i] 3. return Partition(A, p, r)  random pivot results in reasonably balanced split on average ➨ expected running time Θ(n log n)  alternative: use linear time median finding to find a good pivot ➨ worst case running time Θ(n log n) price to pay: added complexity

Graphs

Networks and other graphs road networkcomputer network execution order for processes process 1 process 2 process 3 process 5 process 4 process 6

Graphs: Basic definitions and terminology  A graph G is a pair G = (V, E) V is the set of nodes or vertices of G E ⊂ VxV is the set of edges or arcs of G  If (u, v) ∈ E then vertex v is adjacent to vertex u directed graph (u, v) is an ordered pair ➨ (u, v) ≠ (v, u) self-loops possible undirected graph (u, v) is an unordered pair ➨ (u, v) = (v, u) self-loops forbidden

Some special graphs Tree connected, undirected, acyclic graph  Every tree with n vertices has exactly n-1 edges DAG directed, acyclic graph in-degree number of incoming edges out-degree number of outgoing edges every DAG has a vertex with in-degree 0 and with out-degree 0 …

Topological sort

Input: directed, acyclic graph (DAG) G = (V, E) Output: a linear ordering of v 1,v 2,…, v n of the vertices such that if (v i,v j ) ∈ E then i < j  DAGs are useful for modeling processes and structures that have a partial order Partial order a > b and b > c ➨ a > c but may have a and b such that neither a > b nor b > a execution order for processes process 1 process 2 process 3 process 5 process 4 process 6

Topological sort Input: directed, acyclic graph (DAG) G = (V, E) Output: a linear ordering of v 1,v 2,…, v n of the vertices such that if (v i,v j ) ∈ E then i < j  DAGs are useful for modeling processes and structures that have a partial order Partial order a > b and b > c ➨ a > c but may have a and b such that neither a > b nor b > a a partial order can always be turned into a total order (either a > b or b > a for all a ≠ b) (that’s what a topological sort does …)

Input: directed, acyclic graph (DAG) G = (V, E) Output: a linear ordering of v 1,v 2,…, v n of the vertices such that if (v i,v j ) ∈ E then i < j  Every directed, acyclic graph has a topological order Topological sort v6v6 v5v5 v2v2 v3v3 v4v4 v1v1

underwear pants belt socks shoes shirt tie jacket watch socksunderwearpantsshoesshirttiebeltjacket

Topological sort underwear pants belt socks shoes shirt tie jacket watch socksunderwearpantsshoesshirttiebeltjacket

Topological sort underwear pants belt socks shoes shirt tie jacket watch socksunderwearpantsshoesshirttiebeltjacket

Topological sort underwear pants belt socks shoes shirt tie jacket watch socksunderwearpantsshoesshirttiebeltjacket

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order underwear pants belt socks shoes shirt tie jacket watch

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order 1 in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order

Topological sort underwear pants belt socks shoes shirt tie jacket watch socksunderwearpantsshoesshirttiebeltjacket

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order 1 in-degree underwear pants belt socks shoes shirt tie jacket watch next linear order Running time?

Graph representation Graph G = (V, E) 1.Adjacency lists array Adj of |V| lists, one per vertex Adj[u] = linked list of all vertices v with (u, v) ∈ E (works for both directed and undirected graphs)

Graph G = (V, E) 1.Adjacency lists array Adj of |V| lists, one per vertex Adj[u] = linked list of all vertices v with (u, v) ∈ E (works for both directed and undirected graphs) Graph representation

Graph G = (V, E) 1.Adjacency lists array Adj of |V| lists, one per vertex Adj[u] = linked list of all vertices v with (u, v) ∈ E 2.Adjacency matrix |V| x |V| matrix A = (a ij ) (also works for both directed and undirected graphs) Graph representation a ij = 1 if (i, j) ∈ E 0 otherwise

Graph G = (V, E) 1.Adjacency lists array Adj of |V| lists, one per vertex Adj[u] = linked list of all vertices v with (u, v) ∈ E 2.Adjacency matrix |V| x |V| matrix A = (a ij ) (also works for both directed and undirected graphs) Graph representation a ij = 1 if (i, j) ∈ E 0 otherwise

Adjacency lists vs. adjacency matrix Adjacency listsAdjacency matrix Space Time to list all vertices adjacent to u Time to check if (u, v) ∈ E Θ(V + E)Θ(V 2 ) Θ(degree(u))Θ(V) Θ(1) Θ(degree(u)) better if the graph is sparse … use V for |V| and E for |E|

Topological sort Topological-Sort(G) Input: a DAG G with vertices 1.. n Output: A linear order of the vertices, such that u appears before v if (u, v) in G 1. Let in-degree[1..n] be a new array 2. Set all values in in-degree to 0 3. For each vertex u A. For each vertex v adjacent to u: i. Increment in-degree[v] 4. Make a list next consisting of all vertices u such that in-degree[u] = 0 5. While next is not empty do the following: A. Delete a vertex from next and call it u B. Add u to the end of the linear order C. For each vertex v adjacent to u: i. Decrement in-degree[v] ii. If in-degree[v] = 0 then insert v into next 6. Return the linear order Running time? DAG G represented in adjacency list next is a linked list G has n vertices and m edges 1. Θ(1) 2. Θ(n) 3. Θ(n+m) 4. Θ(n) 5. Θ(n+m) 6. Θ(1) Θ(n+m)