CS38 Introduction to Algorithms Lecture 2 April 3, 2014.

Slides:



Advertisements
Similar presentations
CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 2 1.
Advertisements

Introduction to Algorithms Graph Algorithms
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Implementing a Graph Implement a graph in three ways: –Adjacency List –Adjacency-Matrix –Pointers/memory for each node (actually a form of adjacency list)
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Graphs - II Algorithms G. Miller V. Adamchik CS Spring 2014 Carnegie Mellon University.
CS203 Lecture 15.
Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS
Single Source Shortest Paths
Greedy Algorithms Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
1 Data Structures DFS, Topological Sort Dana Shapira.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CSE 421 Algorithms Richard Anderson Lecture 4. What does it mean for an algorithm to be efficient?
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
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.
Important Problem Types and Fundamental Data Structures
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
CS38 Introduction to Algorithms Lecture 3 April 8, 2014.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
Introduction to Algorithms
Breadth-First Search (BFS)
Chapter 22 Elementary Graph Algorithms
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.
School of Computing Clemson University Fall, 2012
CSE 2331/5331 Topic 9: Basic Graph Alg.
Graph Search Algorithms
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Richard Anderson Autumn 2016 Lecture 5
Richard Anderson Winter 2009 Lecture 6
Important Problem Types and Fundamental Data Structures
Elementary Graph Algorithms
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
Presentation transcript:

CS38 Introduction to Algorithms Lecture 2 April 3, 2014

CS38 Lecture 22 Outline graph traversals (BFS, DFS) connectivity topological sort strongly connected components heaps and heapsort greedy algorithms…

Graphs Graph G = (V, E) –directed or undirected –notation: n = |V|, m = |E| (note: m · n 2 ) –adjacency list or adjacency matrix April 3, 2014CS38 Lecture 23 a a b b c c a a b b c c c c b b b b a b c a b c

Graphs Graphs model many things… –physical networks (e.g. roads) –communication networks (e.g. internet) –information networks (e.g. the web) –social networks (e.g. friends) –dependency networks (e.g. topics in this course) … so many fundamental algorithms operate on graphs April 3, 2014CS38 Lecture 24

Graphs Graph terminology: –an undirected graph is connected if there is a path between each pair of vertices –a tree is a connected, undirected graph with no cycles; a forest is a collection of disjoint trees –a directed graph is strongly connected if there is a path from x to y and from y to x, 8 x,y2V –a DAG is a Directed Acyclic Graph April 3, 2014CS38 Lecture 25

Graph traversals Graph traversal algorithm: visit some or all of the nodes in a graph, labeling them with useful information –breadth-first: useful for undirected, yields connectivity and shortest-paths information –depth-first: useful for directed, yields numbering used for topological sort strongly-connected component decomposition April 3, 2014CS38 Lecture 26

Breadth first search BFS(undirected graph G, starting vertex s) 1.for each vertex v, v.color = white, v.dist = 1, v.pred = nil 2.s.color = grey, s.dist = 0, s.pred = nil 3.Q = ;; ENQUEUE(Q, s) 4.WHILE Q is not empty u = DEQUEUE(Q) 5. for each v adjacent to u 6. IF v.color = white THEN 7. v.color = grey, v.dist = u.dist + 1, v.pred = u 8. ENQUEUE(Q, v) 9. u.color = black Lemma: BFS runs in time O(m + n), when G is represented by an adjacency list. April 3, 20147CS38 Lecture 2

BFS example from CLRS 8

s Breadth first search Lemma: for all v 2 V, v.dist = distance(s, v), and a shortest path from s to v is a shortest path from s to v.pred followed by edge (v.pred,v) Proof: partition V into levels –L 0 = {s} –L i = {v : 9 u 2 L i-1 such that (u,v) 2 E} –Observe: distance(s,v) = i, v 2 L i L0L0 L1L1 L2L2 LnLn edges only within layers or between adjacent layers

Breadth first search Claim: at any point in operation of algorithm: 1. black/grey vertices exactly L 0, L 1, …, L i and part of L i+1 2. Q = (v 0, v 1, v 2, v 3, …, v k ) and all have v.dist = level of v holds initially: s.color = grey, s.dist = 0, Q = (s) April 3, 2014CS38 Lecture 210 s L0L0 L1L1 L2L2 LnLn edges only within layers or between adjacent layers level i level i+1

Breadth first search Claim: at any point in operation of algorithm: 1. black/grey vertices exactly L 0, L 1, …, L i and part of L i+1 2. Q = (v 0, v 1, v 2, v 3, …, v k ) and all have v.dist = level of v 1 step: dequeue v 0 ; add white nbrs of v 0 w/ dist = v 0.dist + 1 April 3, 2014CS38 Lecture 211 s L0L0 L1L1 L2L2 LnLn edges only within layers or between adjacent layers level i level i+1 ) level ¸ i+1 ) level · i+1

Depth first search Lemma: DFS runs in time O(m + n), when G is represented by an adjacency list. Proof? DFS(directed graph G) 1.for each vertex v, v.color = white, v.pred = nil 2.time = 0 3.for each vertex u, IF u.color = white THEN DFS-VISIT(G, u) DFS-VISIT(directed graph G, starting vertex u) 1.time = time +1, u.discovered = time, u.color = grey 2.for each v adjacent to u, IF v.color = white THEN 3. v.pred = u, DFS-VISIT(G, v) 4. u.color = black; time = time + 1; u.finished = time April 3, CS38 Lecture 2

Depth first search Lemma: DFS runs in time O(m + n), when G is represented by an adjacency list. Proof: DFS-VISIT called for each vertex exactly once; its adj. list scanned once; O(1) work DFS(directed graph G) 1.for each vertex v, v.color = white, v.pred = nil 2.time = 0 3.for each vertex u, IF u.color = white THEN DFS-VISIT(G, u) DFS-VISIT(directed graph G, starting vertex u) 1.time = time +1, u.discovered = time, u.color = grey 2.for each v adjacent to u, IF v.color = white THEN 3. v.pred = u, DFS-VISIT(G, v) 4. u.color = black; time = time + 1; u.finished = time April 3, CS38 Lecture 2

Depth first search DFS yields a forest: “the DFS forest” each vertex labeled with discovery time and finishing time edges of G classified as –tree edges –back edges (point back to an ancestor) –forward edges (point forward to a descendant) –cross edges (all others) April 3, 2014CS38 Lecture 214

DFS example from CLRS

DFS application: topological sort Given DAG, list vertices v 0, v 1, …., v n so that no edges from v i to v j (j < i) example: April 3, 2014CS38 Lecture 216 a a b b c c d d f f e e c c e e d d f f b b a a

DFS application: topological sort Theorem: listing vertices in reverse order of DFS finishing times yields a topological sort of DAG G (can implement in linear time; how?) Proof: claim for all (u,v) 2 E, v.finish < u.finish –when (u,v) explored, v not grey since then G would have a cycle [back-edge] –v white ) descendent of u so v finishes first –v black ) already done, so v.finish is set and u.finish will be set with a later time April 3, 2014CS38 Lecture 217

Strongly connected components say that x » y if there is a directed path from x to y and from y to x in G equivalence relation, equivalence classes are strongly connected components of G – also, maximal strongly connected subsets SCC structure is a DAG (why?) April 3, 2014CS38 Lecture 218

Strongly connected components DFS tree from v in G: all nodes reachable from v DFS tree from v in G T : all nodes that can reach v Key: in sink SCC, this is exactly the SCC April 3, 2014CS38 Lecture 219 G with edges reversed v

Strongly connected components given v in a sink SCC, run DFS starting there, then move to next in reverse topological order… –DFS forest would give the SCCs Key #2: topological ordering consistent with SCC DAG structure! (why?) April 3, 2014CS38 Lecture 220 v

Strongly connected components running time O(n + m) if G in adj. list –note: step 2 can be done in O(m + n) time trees in DFS forest of the second DFS are the SCCs of G April 3, 2014CS38 Lecture 221 SCC(directed graph G) 1. run DFS(G) 2. construct G T from G 3. run DFS(G T ) but in line 3, consider vertices in decreasing order of finishing times from the first DFS

Strongly connected components Correctness (sketch): –first vertex is in sink SCC, DFS-VISIT colors black, effectively removes –next unvisited vertex is in sink after removal –and so on… April 3, 2014CS38 Lecture 222 SCC(directed graph G) 1. run DFS(G) 2. construct G T from G 3. run DFS(G T ) but in line 3, consider vertices in decreasing order of finishing times from the first DFS

Summary O(m + n) time algorithms for –computing BFS tree from v in undirected G –finding shortest paths from v in undirected G –computing DFS forest in directed G –computing a topological ordering of a DAG –identifying the strongly connected components of a directed G (all assume G given in adjacency list format) April 3, 2014CS38 Lecture 223

Heaps A basic data structure beyond stacks and queues: heap –array of n elt/key pairs in special order –min-heap or max-heap operations: INSERT(H, elt) INCREASE-KEY(H, i) EXTRACT-MAX(H) April 3, 2014CS38 Lecture 224

Heaps A basic data structure beyond stacks and queues: heap –array of n elt/key pairs in special order –min-heap or max-heap operations:time: INSERT(H, elt)O(log n) INCREASE-KEY(H, i)O(log n) EXTRACT-MAX(H)O(log n) April 3, 2014CS38 Lecture 225

Heaps array A represents a binary tree that is full except for possibly last “row” heap property: A[parent(i)] ¸ A[i] for all i April 3, 2014CS38 Lecture 226 parent(i) = bi/2c left(i) = 2i right(i) = 2i+1 height = O(log n)

Heaps key operation: HEAPIFY-DOWN(H, i) April 3, 2014CS38 Lecture 227 A[i] may violate heap property –repeatedly swap with larger child –running time? O(log n) or O(ht)

Heaps key operation: HEAPIFY-UP(H, i) April 3, 2014CS38 Lecture 228 A[i] may violate heap property –repeatedly swap with larger child –running time? O(log n) or O(ht)

Heaps How do you implement operations:time: INSERT(H, elt)O(log n) INCREASE-KEY(H, i)O(log n) EXTRACT-MAX(H)O(log n) using HEAPIFY-UP and HEAPIFY-DOWN? BUILD-HEAP(A): re-orders array A so that it satisfies heap property –how do we do this? running time? April 3, 2014CS38 Lecture 229

Heaps BUILD-HEAP(A): re-orders array A so that it satisfies heap property –call HEAPIFY-DOWN(H, i) for i from n downto 1 –running time O(n log n) –more careful analysis: O(n) April 3, 2014CS38 Lecture 230

Heaps suffices to show  h¸0 h/2 h = O(1) note:  h¸0 c h = O(1) for c < 1 observe: (h+1)/2 h+1 = h/(2 h ) ¢ (1+1/h)/2 (1+1/h)/2 1 April 3, 2014CS38 Lecture 231

Heapsort Sorting n numbers using a heap –BUILD-HEAP(A)O(n) –repeatedly EXTRACT-MIN(H)n¢O(log n) –total O(n log n) Can we do better? O(n)? –observe that only ever compare values –no decisions based on actual values of keys April 3, 2014CS38 Lecture 232

Sorting lower bound comparison-based sort: only information about A used by algorithm comes from pairwise comparisons –heapsort, mergesort, quicksort, … April 3, 2014CS38 Lecture 233 visualize sequence of com- parisons in tree: “is A[i] · A[j]?” each root-leaf path consistent with 1 perm. maximum path length ¸ log(n!) =  (n log n)

Greedy Algorithms April 3, 2014CS38 Lecture 234

Greedy algorithms Greedy algorithm paradigm –build up a solution incrementally –at each step, make the “greedy” choice Example: in undirected graph G = (V,E), a vertex cover is a subset of V that touches every edge –a hard problem: find the smallest vertex cover April 3, 2014CS38 Lecture 235 a a b b c c d d f f e e a a b b c c d d f f

Dijkstra’s algorithm given –directed graph G = (V,E) with non-negative edge weights –starting vertex s 2 V find shortest paths from s to all nodes v –note: unweighted case solved by BFS April 3, 2014CS38 Lecture 236

Dijkstra’s algorithm shortest paths exhibit “optimal substructure” property –optimal solution contains within it optimal solutions to subproblems –a shortest path from x to y via z contains a shortest path from x to z shortest paths from s form a tree rooted at s Main idea: –maintain set S µ V with correct distances –add nbr u with smallest “distance estimate” April 3, 2014CS38 Lecture 237