Download presentation
Presentation is loading. Please wait.
Published byKelly Barton Modified over 9 years ago
1
2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs
2
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
3
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
4
QuickSort One more sorting algorithm …
5
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)
6
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.
7
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)
8
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???
9
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+1 81640395 p ij r 81640395 p ij r 18640395 p ij r 14680395 p ij r 18640395 p ij r 14086395 p ij r 14036895 p ij r 14036895 p i r 14035896 p i r
10
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
11
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
12
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
13
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 …
14
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
15
Graphs
16
Networks and other graphs road networkcomputer network execution order for processes process 1 process 2 process 3 process 5 process 4 process 6
17
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
18
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 …
19
Topological sort
20
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
21
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 …)
22
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
23
underwear pants belt socks shoes shirt tie jacket watch socksunderwearpantsshoesshirttiebeltjacket
24
Topological sort underwear pants belt socks shoes shirt tie jacket watch socksunderwearpantsshoesshirttiebeltjacket
25
Topological sort underwear pants belt socks shoes shirt tie jacket watch socksunderwearpantsshoesshirttiebeltjacket
26
Topological sort underwear pants belt socks shoes shirt tie jacket watch socksunderwearpantsshoesshirttiebeltjacket
27
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 1 2 3 4 6 5 7 8 9
28
123456789 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 2 3 4 6 5 7 8 9 in-degree underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9
29
123456789 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 2 3 4 6 5 7 8 9 in-degree 000000000 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9
30
123456789 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 2 3 4 6 5 7 8 9 in-degree 001010000 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9
31
123456789 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 2 3 4 6 5 7 8 9 in-degree 002010000 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9
32
123456789 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 2 3 4 6 5 7 8 9 in-degree 003011000 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9
33
123456789 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 2 3 4 6 5 7 8 9 in-degree 003011001 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9
34
123456789 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 2 3 4 6 5 7 8 9 in-degree 003012011 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9
35
123456789 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 2 3 4 6 5 7 8 9 in-degree 003012012 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9
36
123456789 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 2 3 4 6 5 7 8 9 in-degree 003012012 1247 7 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
37
123456789 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 2 3 4 6 5 7 8 9 in-degree 003011002 1247 7 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
38
123456789 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 2 3 4 6 5 7 8 9 in-degree 003011002 1248 78 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
39
123456789 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 2 3 4 6 5 7 8 9 in-degree 003011001 1248 78 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
40
123456789 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 2 3 4 6 5 7 8 9 in-degree 003011001 124 7842 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
41
123456789 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 2 3 4 6 5 7 8 9 in-degree 002011001 12 7842 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
42
123456789 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 2 3 4 6 5 7 8 9 in-degree 002011001 1 78421 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
43
123456789 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 2 3 4 6 5 7 8 9 in-degree 001001001 1 78421 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
44
123456789 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 2 3 4 6 5 7 8 9 in-degree 001001001 5 784215 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
45
123456789 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 2 3 4 6 5 7 8 9 in-degree 000000001 5 784215 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
46
123456789 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 2 3 4 6 5 7 8 9 in-degree 000000001 36 7842156 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
47
123456789 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 2 3 4 6 5 7 8 9 in-degree 000000000 36 7842156 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
48
123456789 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 000000000 39 784215693 2 3 4 6 5 7 8 9 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order
49
Topological sort underwear pants belt socks shoes shirt tie jacket watch socksunderwearpantsshoesshirttiebeltjacket
50
123456789 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 000000000 784215693 2 3 4 6 5 7 8 9 underwear pants belt socks shoes shirt tie jacket watch 1 2 3 4 6 5 7 8 9 next linear order Running time?
51
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) 5 4 3 2 1 3 3 14 1 5 4 3 2 2 2 3 1
52
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 5 4 3 2 1 1 1 5 4 3 2 2 2 3
53
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 1 2 3 4 5 31245 1 1 1 1 1 1 1 1 5 4 3 2 1
54
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 1 2 3 4 5 31245 1 1 1 1 5 4 3 2 1
55
Adjacency lists vs. adjacency matrix 3 3 14 1 5 4 3 2 2 2 3 1 1 2 3 4 5 31245 1 1 1 1 1 1 1 1 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|
56
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.