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)