Design and Analysis of Algorithms

Similar documents
Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

Graphs: Connectivity. Jordi Cortadella and Jordi Petit Department of Computer Science

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

CSE 101. Algorithm Design and Analysis Miles Jones and Russell Impagliazzo Miles Office 4208 CSE Building

Graph Algorithms Using Depth First Search

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graph Representations and Traversal

Design and Analysis of Algorithms

Graph: representation and traversal

Graph Representation

Graphs: Connectivity. A graph. Transportation systems. Social networks

Lecture 3: Graphs and flows

CS483 Design and Analysis of Algorithms

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Minimum Spanning Trees Ch 23 Traversing graphs

Data Structures and Algorithms. Chapter 7. Graphs

Announcements. HW3 is graded. Average is 81%

CSCE 350: Chin-Tser Huang. University of South Carolina

Graph. Vertex. edge. Directed Graph. Undirected Graph

Outline. Graphs. Divide and Conquer.

Copyright 2000, Kevin Wayne 1

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs

CSC Intro to Intelligent Robotics, Spring Graphs

CMSC 451: Lecture 22 Approximation Algorithms: Vertex Cover and TSP Tuesday, Dec 5, 2017

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Chapter 9 Graph Algorithms

CSE 101- Winter 18 Discussion Section Week 2

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016

CSE 417 Branch & Bound (pt 4) Branch & Bound

Lecture 8: The Traveling Salesman Problem

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

DFS & STRONGLY CONNECTED COMPONENTS

W4231: Analysis of Algorithms

GRAPHS Lecture 19 CS2110 Spring 2013

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Data Structures and Algorithms. Werner Nutt

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Algorithm Design and Analysis

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Lecture 9 Graph Traversal

CS 310 Advanced Data Structures and Algorithms

1 Digraphs. Definition 1

Exercise 1. D-ary Tree

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Depth First Search (DFS)

EECS 203 Lecture 20. More Graphs

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Design and Analysis of Algorithms

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 9 Notes. Class URL:

CSE 421 DFS / Topological Ordering

Graph Algorithms. Definition

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

CSI 604 Elementary Graph Algorithms

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

Basic Graph Algorithms (CLRS B.4-B.5, )

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Graph representation

2 Approximation Algorithms for Metric TSP

1 The Traveling Salesman Problem

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Goals! CSE 417: Algorithms and Computational Complexity!

COS 423 Lecture14. Robert E. Tarjan 2011

Chapter 22 Elementary Graph Algorithms

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Elements of Graph Theory

MAL 376: Graph Algorithms I Semester Lecture 1: July 24

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Algorithms: Lecture 10. Chalmers University of Technology

val(y, I) α (9.0.2) α (9.0.3)

Graph Search. Adnan Aziz

22.3 Depth First Search

(Refer Slide Time: 05:25)

Representations of Graphs

Homework 5: Graphs, Minimum Spanning Trees, and Dijkstra Shortest-Path

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Algorithm Design and Analysis

Midterm 1 Solutions. (i) False. One possible counterexample is the following: n n 3

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Discrete mathematics II. - Graphs

Final. Name: TA: Section Time: Course Login: Person on Left: Person on Right: U.C. Berkeley CS170 : Algorithms, Fall 2013

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Undirected Graphs. 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

Algorithm Design and Analysis

Graphs and trees come up everywhere. We can view the internet as a graph (in many ways) Web search views web pages as a graph

The Konigsberg Bridge Problem

1 The Traveling Salesperson Problem (TSP)

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Graphs. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

Transcription:

CSE, Winter 8 Design and Analysis of Algorithms Lecture : Graphs, DFS (Undirected, Directed), DAGs Class URL: http://vlsicad.ucsd.edu/courses/cse-w8/

Graphs Internet topology

Graphs Gene-gene interactions affecting phenotype in yeast

Graphs Airline routes System dependence graph

Graph G = (V,E) Graph Representation Vertex set V = V(G) Edge set E = E(G) (connecting pairs of vertices) How is this stored in a computer?

Graph Representation Adjacency Matrix Graph G = (V,E) Vertex set V = V(G) Edge set E = E(G) (connecting pairs of vertices) O( V ) storage needed Adjacency List O( V + E ) storage needed

Depth-First Search Depth-first search is a method for exploring a graph Explore deeper in the graph whenever possible Edges are explored out of the most recently discovered vertex v that still has unexplored edges When all of v s edges have been explored, backtrack to the vertex from which v was discovered Analogy: Exploring a maze Mark with chalk: has the vertex been visited already Unwind / rewind ball of string: stack push / pop Pseudocode in book: stack is implicit from recursion

Depth-First Search: Pseudocode Figure. Finding all nodes reachable from a particular node. procedure explore(g,v) Input: Output: G = (V,E); v V visited(u) set to true for all nodes u reachable from v visited(v) = true previsit(v) // pre[v] = clock; clock++; for each edge (v,u) in E: if not visited(u): explore(u) postvisit(v) // post[v] = clock; clock++; A E I B J B,, A E I J,9,8 6,7

Depth-First Search: Pseudocode Figure. Finding all nodes reachable from a particular node. procedure explore(g,v) Input: Output: G = (V,E); v V visited(u) set to true for all nodes u reachable from v visited(v) = true previsit(v) // pre[v] = clock; clock++; ccnum[v] = cc for each edge (v,u) in E: if not visited(u): explore(u) postvisit(v) // post[v] = clock; clock++; Figure. Depth-first search. procedure dfs (G) // clock = ; cc = ; for all v V: visited(v) = false for all v V: if not visited(v): cc++; explore(v)

Depth-First Search: Example (Fig..6) A B C D, A, C, F E I F J G K H L B, CC E I J,9,8 6,7,7 G K,6 D H CC,, L 8,9 CC Tree edge: when encountering a new vertex Back edge: when encountering a previously-seen vertex (i.e., from descendant to ancestor)

Depth-First Search: Notes pre[v] is the step when a vertex is first touched, and post[v] is the step when a vertex is last touched Correspond to preorder and postorder listings of nodes Preorder of CC: A-B-E-I-J; Postorder of CC: B-J-I-E-A A connected component is a maximal subgraph that is connected but has no edges to vertices outside the subgraph cc counts connected components. DFS runtime is O( V + E ) O( V ) for marking each node as visited, pre/post-visit operations O( E ) total work for traversing edges: each edge (x,y) E is examined exactly twice, once during explore(x) and once during explore(y) QUESTION: Does DFS correctly reach all reachable v s?

Depth-First Search: Example (Fig..6) A B C D, A, C, F E F G H I J K L B, CONNECTED COMPONENTS - Linear-time CC finding CC - Traversed edges in a CC form a tree - Trees together constitute a forest E I J,9,8 6,7,7 G K,6 D H CC,, L 8,9 CC Tree edge: when encountering a new vertex Back edge: when encountering a previously-seen vertex (i.e., from descendant to ancestor)

A E I Depth-First Search: Example (Fig..6) B F J C G K D H L B, PRE- and POST-ORDERINGS For all u,v the intervals [pre(u),post(u)] and [pre(v),post(v)] are either disjoint or nested [pre(u),post(u)] is essentially the time interval during which node u is on the stack (and, stack has LIFO behavior), A E I J CC,9,8 6,7,7 G K,6, C D H CC,, L 8,9, F CC Tree edge: when encountering a new vertex Back edge: when encountering a previously-seen vertex (i.e., from descendant to ancestor)

DFS in DIRECTED Graphs A is root of tree; all other nodes are A s descendants E has descendants F, G, H (E is an ancestor of G) C is the parent of D H is a child of E TERMINOLOGY

More Edge Types Tree edges = part of the DFS forest Forward edges = from a node to a non-child descendant Back edges = to an ancestor in the tree Cross edges = to neither descendant nor ancestor EDGE TYPES

Ancestry and Pre/Post Numbers Node u is an ancestor of node v iff pre(u) < pre(v) < post(u) Because u is an ancestor of v iff u is discovered first, and then v is encountered during the exploration of u [Def. Node v is a descendant of u iff node u is an ancestor of v]

Edge Types by Pre/Post Numbers Edge Type Pre/Post Properties of (u,v) In Search Tree? Tree pre(u) < pre(v) < post(v) < post(u) Yes Forward pre(u) < pre(v) < post(v) < post(u) No Back pre(v) < pre(u) < post(u) < post(v) No Cross pre(v) < post(v) < pre(u) < post(u) No Every edge must fall into one of these groups

Cycles in Directed Graphs Definition: A cycle in a directed graph is a circular path v v v v k v If a graph has no cycles, then it is an acyclic graph A directed graph has a cycle iff DFS reveals a back edge ( ) If (u,v) is a back edge, then it along with the v u path in the search tree will form a cycle. ( ) If the graph has a cycle v v v v k v then consider the node with smallest pre number, call it v i. All other v j on the cycle are reachable from v i and will therefore be descendants of v i in the search tree. Thus, the edge v i- v i is a back edge. So, we can determine whether G is acyclic in linear time

Topological Ordering and DAGs Prelude to shortest paths Generic scheduling problem Input: Set of tasks {T, T, T,, T n } Example: getting dressed in the morning: put on shoes, socks, shirt, pants, belt, Set of dependencies {T T, T T, T T, } Example: must put on socks before shoes, pants before belt, Want: ordering of tasks which is consistent with dependencies Problem representation: Directed Acyclic Graph Vertices = tasks; Directed Edges = dependencies Acyclic: if cycle of dependencies, no solution possible General model for causality, dependency

Topological Ordering TOP_ORDER PROBLEM: Given a DAG G = (V,E) with V =n, assign labels,...,n to v i V s.t. if v has label k, all vertices reachable from v have labels > k Every edge is from a lower label to a higher label

Topological Ordering TOP_ORDER PROBLEM: Given a DAG G = (V,E) with V =n, assign labels,...,n to v i V s.t. if v has label k, all vertices reachable from v have labels > k Every edge is from a lower label to a higher label Induction idea : Know how to label DAG s with < n vertices Claim: A DAG G always has some vertex with indegree = Take an arbitrary vertex v. If v doesn t have indegree =, traverse any incoming edge to reach a predecessor of v. If this vertex doesn t have indegree =, traverse any incoming edge to reach a predecessor, etc. Eventually, this process will either identify a vertex with indegree =, or else reach a vertex that has been reached previously (a contradiction, given that G is acyclic). Inductive approach : Find v with indegree(v) =, give it lowest available label, then delete v (and incident edges), update degrees of remaining vertices, and repeat

Topological Ordering TOP_ORDER PROBLEM: Given a DAG G = (V,E) with V =n, assign labels,...,n to v i V s.t. if v has label k, all vertices reachable from v have labels > k Every edge is from a lower label to a higher label Solution: Run DFS, then perform tasks in decreasing order of post numbers Fact: In a DAG, every edge leads to a vertex with lower post number. Why? Any edge (u,v) for which post(v) > post(u) is a back edge. But a DAG, being acyclic, has no back edges.

Sources and Sinks In a DAG, the node with smallest post number has no outgoing edges Such a node is called a sink In a DAG, the node with largest post number has no incoming edges Such a node is called a source

Administrative Notes, January You are strongly urged to attend at least one discussion section per week. On exams, you are responsible for material that is (i) presented by TAs and (ii) posted on the class website as Discussion Notes Reminder Apologies for not getting to this slide The first point was answered in class, and hopefully everything else is already clear! HW # is due next Friday, January 9, on Gradescope Deadline =:9pm Pacific Time HW #, PA # pipecleaners are due by tomorrow, :9pm PT HW review ression tomorrow, Friday pm in WLH PA # (on graph traversal) due on Friday, January 6 at :9pm PT. Next Friday, January 9, pm: PA review session Podcasting for Friday review sessions has been requested from ACMS Being a member of your professional society has many, many benefits access to networking, access to latest technology / industry news, etc. Cost = $9 = less than a pizza! How many of you are student members of ACM?

I will try to post narratives / walk-throughs of Fun Problems and Extra Material likely under Resources in Piazza. I mentioned to one student after class that if treatment of one topic is of particular interest, please make a request on Piazza so that I can prioritize it Thanks! EXTRA MATERIAL : Triangle-Finding by Matrix Multiplication? Definition of EXTRA MATERIAL : Of current or future relevance and interest, but unless included in a lecture, you are not responsible for it on exams Note: unless included in a lecture, you are not responsible this material really is extra unless called out otherwise

Finding Triangles Given a graph G = (V,E), count the number of triangles in G Vertices v i, v j, v k V form a triangle iff edges (v i,v j ), (v j,v k ), (v i,v k ) E QUESTION: What is an O(n ) algorithm? QUESTION: Would the algorithm complexity be different if I asked you to list all triangles in G? Or, if I asked you to determine whether there is a triangle in G? Worst-case complexity can depend on size of output

A Property of the Adjacency Matrix A Let A = the adjacency matrix of G What is the meaning of A ij? A A X = A ii = deg(v i ) A ij = # of length- paths from v i to v j in G

Finding Triangles A Let A = the adjacency matrix of G We can use A, A to find triangles in G! (How much time does it take to compute A?) A A X =

What s Different Here? Adjacency List Adjacency Matrix Graph G = (V,E) Vertex set V = V(G) Edge set E = E(G) (connecting pairs of vertices) Directed Graph

EXTRA MATERIAL : Illustration of the Celebrity Problem (see Lecture, Slide )

The Celebrity Problem Finding a Celebrity. Given a set S of N people, assume that for any pair I, J exactly one of the following is true: I knows J, or J knows I. Further, define a celebrity as someone who knows no one (and who is therefore known by everyone else). Given the knows relation over S, determine whether S contains a celebrity. Celebrity Problem: directed graph whose adjacency matrix has for each i,j pair either (A ij =, A ji = (=J knows I)) or (A ij =, A ji = (= I knows J)) Adjacency Matrix of a Directed Graph ( Person # is a Celebrity) Question: What is a practical application for this problem? Can you determine existence of a celebrity in O(n ) time? O(n)?

EXTRA MATERIAL : DFS in a Minimum Spanning Tree == A Heuristic for the Geometric Traveling Salesperson Problem

Side Note: Concept of Approximation Algorithm Approximation algorithm: An algorithm that returns near-optimal solutions (i.e. is "provably good ) is called an approximation algorithm. Performance Ratio (Ratio Bound): We say that an approximation algorithm for the problem has a ratio bound of (n) if for any instance I of size n, the cost C of the solution produced by the approximation algorithm is within a factor of (n) of the cost C * of an optimal solution: max I = n (C/C * ) (n)

Approximation Algorithm : Euclidean TSP Euclidean Traveling Salesperson Problem: Let C,C,,C n be a set of points in the plane corresponding to the location of n cities. Find a minimum-distance Hamiltonian cycle (traveling salesman tour) among them. TSP is a hard problem, but there is a minimum spanning tree (MST) based approximation with performance ratio = Fact: cost(mst) < cost(tour opt ) MST is the minimum-cost graph that connects all vertices, and has only n- edges Any TSP tour must also connect all vertices, and will have n edges Notice that a tour can be viewed as a spanning tree (that happens to be a chain) plus another edge

Approximation Algorithm : Euclidean TSP Idea: Consider the circuit that consists of a DFS traversal of MST (starting from any city), and includes an edge in the opposite direction whenever the search backtracks. And then we can take shortcut on the tour we get. Next slide is an example: DFS traversal starting from city a produces a circuit a-b-c-b-a-d We can then use a shortcut c-d to replace original path c-b-ad. Note: Being in a metric space (Euclidean is just one possibility) means that the triangle inequality holds, which means that the shortcuts reduce tour cost.

Approximation Algorithm: Euclidean TSP d a b c DFS traversal of MST Tour Heur * MST * Tour opt Taking shortcut from DFS tour. (e.g. replacing a-b-c-b-a-d, by a-b-c-d)