Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 4.

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS
Lecture 7 March 1, 11 discuss HW 2, Problem 3
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
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 A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
Graphs. Data structures that connect a set of objects to form a kind of a network Objects are called “Nodes” or “Vertices” Connections are called “Edges”
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 9 Instructor: Paul Beame.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
1 Graphs - Definitions - Breadth First Search - Depth First Search - Connectivity - Topological Ordering.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
CSCI 256 Data Structures and Algorithm Analysis Lecture 4 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 10.
Representing and Using Graphs
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 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.
3.1 Basic Definitions and Applications. Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
3.1 Basic Definitions and Applications
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Graph Theory and Applications
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Topic 12 Graphs 1. Graphs Definition: Two types:
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 6.
1 Chapter 3 Graphs Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Graphs and Paths : Chapter 15 Saurav Karmakar
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Introduction to Algorithms
Data Structures and Algorithm Analysis Lecture 5
3.1 Basic Definitions and Applications
CSC317 Graph algorithms Why bother?
Chapter 3 Graphs Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
CSE 421: Introduction to Algorithms
Basic Graph Algorithms
CSE 421: Introduction to Algorithms
Searching in Graphs.
Graphs Definitions Breadth First Search Depth First Search
3.2 Graph Traversal.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Algorithm Course Dr. Aref Rashad
Presentation transcript:

Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 4

Q3: Analyzing an algorithm You have an array A with integer entries A[1],…, A[n] Output a 2-D array B such that B[i,j] contains the sum A[i] + A[i+1] + … + A[j] Here is an algorithm: For i=1, 2, …, n For j = i+1, 2, …, n { Add up entries A[i] through A[j] Store result in B[i,j] } Obtain an upper bound and a lower bound for the algorithm. 2

The lower bound is the more interesting one Consider the times during the execution of the algorithm when i ≤ n/4 and j ≥ 3n/4. In these cases, j − i + 1 ≥ 3n/4 − n/4 + 1 > n/2. Therefore, adding up the array entries A[i] through A[j] would require at least n/2 operations, since there are more then n/2 terms to add up. How many times during the execution of the given algorithm do we encounter such cases? There are (n/4) 2 pairs (i, j) with i ≤ n/4 and j ≥ 3n/4. the given algorithm enumerates over all of them, and as shown above, it must perform at least n/2 operations for each such pair. Therefore, the algorithm must perform at least n/2.(n/4) 2 = n 3 /32 operations. This is Ω(n 3 ), as desired. 3

Graphs 3.1 Basic Definitions and Applications

5 Undirected Graphs Undirected graph. G = (V, E) n V = nodes. n E = edges between pairs of nodes. n Captures pairwise relationship between objects. n Graph size parameters: n = |V|, m = |E|. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

6 Some Graph Applications transportation Graph street intersections NodesEdges highways communicationcomputersfiber optic cables World Wide Webweb pageshyperlinks socialpeoplerelationships food webspeciespredator-prey software systemsfunctionsfunction calls schedulingtasksprecedence constraints circuitsgateswires

7 World Wide Web Web graph. n Node: web page. n Edge: hyperlink from one page to another. cnn.com cnnsi.com novell.comnetscape.com timewarner.com hbo.com sorpranos.com

Terrorist Network Social network graph. n Node: people. n Edge: relationship between two people. Reference: Valdis Krebs,

9 Ecological Food Web Food web graph. n Node = species. n Edge = from prey to predator. Reference:

10 Graph Representation: Adjacency Matrix Adjacency matrix. n-by-n matrix with A uv = 1 if (u, v) is an edge. n Two representations of each edge. n Space proportional to n 2. n Checking if (u, v) is an edge takes  (1) time. n Identifying all edges takes  (n 2 ) time

11 Graph Representation: Adjacency List Adjacency list. Node indexed array of lists. n Two representations of each edge. n Space proportional to m + n. n Checking if (u, v) is an edge takes O(deg(u)) time. n Identifying all edges takes  (m + n) time degree = number of neighbors of u 37

12 Paths and Connectivity Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v 1, v 2, …, v k-1, v k with the property that each consecutive pair v i, v i+1 is joined by an edge in E. Def. A path is simple if all nodes are distinct. Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v.

13 Cycles Def. A cycle is a path v 1, v 2, …, v k-1, v k in which v 1 = v k, k > 2, and the first k-1 nodes are all distinct. cycle C =

14 Trees Def. An undirected graph is a tree if it is connected and does not contain a cycle. Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third. n G is connected. n G does not contain a cycle. n G has n-1 edges.

15 Rooted Trees Rooted tree. Given a tree T, choose a root node r and orient each edge away from r. Importance. Models hierarchical structure. a tree the same tree, rooted at 1 v parent of v child of v root r

16 Phylogeny Trees Phylogeny trees. Describe evolutionary history of species.

17 GUI Containment Hierarchy Reference: GUI containment hierarchy. Describe organization of GUI widgets.

3.2 Graph Traversal

19 Connectivity s-t connectivity problem. Given two nodes s and t, is there a path between s and t? s-t shortest path problem. Given two nodes s and t, what is the length of the shortest path between s and t? Applications. n Facebook. n Maze traversal. n Erdos number. n Kevin Bacon number. n Fewest number of hops in a communication network.

20 Breadth First Search BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time. Effect: find “shallow” paths to nodes. BFS algorithm. n L 0 = { s }. n L 1 = all neighbors of L 0. n L 2 = all nodes that do not belong to L 0 or L 1, and that have an edge to a node in L 1. n L i+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in L i. Theorem. For each i, L i consists of all nodes at distance exactly i from s. There is a path from s to t iff t appears in some layer. s L1L1 L2L2 L n-1

Implementing BFS Q: What’s a good way to implement the above algorithm? A: Use a queue for the “frontier” 21

22 Breadth First Search Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of G. Then the level of x and y differ by at most 1. L0L0 L1L1 L2L2 L3L3

23 Breadth First Search: Analysis Theorem. The above implementation of BFS runs in O(m + n) time if the graph is given by its adjacency list representation. Pf. n Easy to prove O(n 2 ) running time: – at most n lists L i – each node occurs on at most one list; for loop runs  n times – when we consider node u, there are  n incident edges (u, v), and we spend O(1) processing each edge n Actually runs in O(m + n) time: – when we consider node u, there are deg(u) incident edges (u, v) – total time processing edges is  u  V deg(u) = 2m ▪ each edge (u, v) is counted exactly twice in sum: once in deg(u) and once in deg(v)

24 Connected Component Connected component. Find all nodes reachable from s. Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }.

Q1: Finding connected components Give an algorithm to find the set of all connected components of an undirected graph. 25

26 Connected Component Connected component. Find all nodes reachable from s. Theorem. Upon termination, R is the connected component containing s. n BFS = explore in order of distance from s. s uv R it's safe to add v

27 Q2: Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. recolor lime green blob to blue

28 Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. recolor lime green blob to blue

29 Flood Fill Flood fill. Given lime green pixel in an image, change color of entire blob of neighboring lime pixels to blue. n Node: pixel. n Edge: two neighboring lime pixels. n Blob: connected component of lime pixels. recolor lime green blob to blue

Depth-first search Use recursion DFS intuition. Explore outward from s along one path as far as possible, and backtrack when you cannot progress. Effect: find faraway nodes. DFS(u): Mark u as “Explored” and add u to R For each edge (u,v) incident to u If v is not marked “Explored” then Recursively call DFS(v) 30

Depth-first search Property. For a given recursive call DFS(u), all nodes marked “Explored” between the beginning and end of this recursive call are descendants of u in T. Theorem. Let T be a depth-first search tree, let x and y be nodes in T, and let (x,y) be an edge of G that is not an edge of T. Then one of x or y is an ancestor of the other. 31

Q3: BFS and DFS trees We have a connected graph G = (V, E) and a specific vertex u. Suppose we compute a DFS tree rooted at u, and obtain a tree T that includes all nodes of G. Suppose we then compute a BFS tree rooted at u, and obtain the same tree T. Prove that G = T. 32

Answer Suppose G has an edge e = {a, b} that does not belong to T. As T is a DFS tree, one of the two ends must be an ancestor of the other—say a is an ancestor of b. (*) Since T is a BFS tree, the distance of the two nodes from u in T can differ at most by one. But if a is an ancestor of b, and (*) holds, then a must be the direct parent of b. This means that {a, b} is an edge in T. Contradiction. 33

Q4: Finding a cycle Given a graph G, determine if it has a cycle. If so, the algorithm should output this cycle. Answer: Assume that G is connected; otherwise work on the connected components. Run BFS from an arbitrary node s, and obtain a BFS tree T. If every edge of G appears in the tree, then G = T and there is no cycle. Otherwise, there is an edge e = (v, w) that is in G but not in T. Consider the least common ancestor u of v and w in T. We get a cycle from edge e and paths u-v and u-w in T. 34