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

Similar documents
Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Trees. Tree Structure Binary Tree Tree Traversals

Graph. Vertex. edge. Directed Graph. Undirected Graph

CS 310 Advanced Data Structures and Algorithms

Data Structure. IBPS SO (IT- Officer) Exam 2017

Undirected Graphs. Hwansoo Han

Elementary Graph Algorithms CSE 6331

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

Algorithms: Lecture 10. Chalmers University of Technology

Lecture 3: Graphs and flows

Design and Analysis of Algorithms

Graph Algorithms Using Depth First Search

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

DS UNIT 4. Matoshri College of Engineering and Research Center Nasik Department of Computer Engineering Discrete Structutre UNIT - IV

Search Algorithms. IE 496 Lecture 17

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

Copyright 2000, Kevin Wayne 1

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)

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory


CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Name CPTR246 Spring '17 (100 total points) Exam 3

Definition Example Solution

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Graph Representation

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

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

Graph: representation and traversal

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Searching in Graphs (cut points)

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Graph representation

Representations of Graphs

CSC Intro to Intelligent Robotics, Spring Graphs

(Refer Slide Time: 02.06)

CS2 Algorithms and Data Structures Note 9

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

March 20/2003 Jayakanth Srinivasan,

Algorithm Design and Analysis

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

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

Trees Algorhyme by Radia Perlman

Chapter 22 Elementary Graph Algorithms

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

LECTURE 17 GRAPH TRAVERSALS

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

DFS & STRONGLY CONNECTED COMPONENTS

implementing the breadth-first search algorithm implementing the depth-first search algorithm

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

22.3 Depth First Search

Inf 2B: Graphs, BFS, DFS

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

Data Structures and Algorithms. Chapter 7. Graphs

Design and Analysis of Algorithms

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

Chapter 9 Graph Algorithms

CSI 604 Elementary Graph Algorithms

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Elementary Graph Algorithms

Some Extra Information on Graph Search

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

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

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Data Structures and Algorithms Key to Homework Assignment 8

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Unweighted Graphs & Algorithms

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

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

Basic Graph Algorithms

Design and Analysis of Algorithms

- Logic and Algorithms - Graph Algorithms

Graph Algorithms. Definition

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Algorithm Design and Analysis

COT 6405 Introduction to Theory of Algorithms

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

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

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Konigsberg Bridge Problem

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Introduction and Overview

Lecture 9 Graph Traversal

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

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Chapter 22. Elementary Graph Algorithms

Module 5 Graph Algorithms

Transcription:

Computational Optimization ISE 407 Lecture 19 Dr. Ted Ralphs

ISE 407 Lecture 19 1 Search Algorithms Search algorithms are fundamental techniques applied to solve a wide range of optimization problems. Search algorithms attempt to find all the nodes in a network satisfying a particular property. Examples Find nodes that are reachable by directed paths from a source node. Find nodes that can reach a specific node along directed paths Identify the connected components of a network Identify directed cycles in network Let us consider undirected graphs to start. We will first consider an algorithm for finding a simple path in a graph.

ISE 407 Lecture 19 2 Connectivity in Graphs An undirected graph is said to be connected if there is a path between any two vertices in the graph. A graph that is not connected consists of a set of connected components that are the maximal connected subgraphs. Given a graph, one of the most basic questions one can ask is whether vertices i and j are in the same component. In other words, is there a path from i to j? Such questions might arise in many applications.

ISE 407 Lecture 19 3 Finding a Simple Path We now revisit the question of whether there is a path connecting a given pair of vertices in a graph. Using the operations in the Graph class, we can answer this question of whether there is a path from i to j using a recursive algorithm. We must pass in a vector of bools to track which nodes have been visited. def SPath(G, v, w, visited = {}) if v == w: return True visited[v] = True for n in v.get_neighbors(): if not visited[n]: if SPath(G, n, w, visited): return True return False

ISE 407 Lecture 19 4 Labeling Components The set of all nodes connected to a given node by a path is called a component. How easy is it to determine all of the nodes in the same component as a given node? def DFSRecursion(G, v, pred, component_num = 0): G.set_node_attr(v, component, component_num) for n in G.get_neighbors(v): if G.get_node_attr(n, component ) == None pred[n] = v DFS(G, n, pred, component_num) return def DFS(G, v, component_num = 0) for n in G.get_node_list(): G.set_node_attr(n, component, None) DFSRecursion(G, v, [], component_num) return

ISE 407 Lecture 19 5 Depth-first Search for General Graphs The algorithm we have just seen is a generalization of the depth-first search method we saw earlier for trees. We will see why it is called this shortly. Associated with the search is a search tree that can be used to visualize the algorithm. At the time a node n is discovered, we can record v as its predecesor. The set of edges consisting of each node and its predecessor forms a tree rooted at v. We call the edges in the tree tree edges. The remaining edges connect a vertex with an ancestor in the tree that is not its parent and are called back edges. Why must every edge be either a tree edge or a back edge?

ISE 407 Lecture 19 6 Complexity of Depth-first Search How do we analyze a DFS algorithm? How many recursive calls are there? How does the graph data structure affect the running time? Adjacency matrix Adjacency list

ISE 407 Lecture 19 7 Node Ordering As in depth-first search for trees, the nodes can be ordered in two ways. Preorder: The order in which the nodes are first discovered (discovery time). Postorder: The order in which the nodes finished (the recursive calls on all neighbors return). These orders will be referred to in various algorithms we ll study.

ISE 407 Lecture 19 8 Labeling All Components To label all components, we loop through all the nodes in the graph and start labeling the component of any node we find that has not already been labeled. def label_component(g): component_num = 0 for n in G.get_node_list(): G.set_node_attr(n, component, None) for n in G.get_node_list(): if G.get_node_attr(n, component ) is None: DFS(G, n, component_num) component_num += 1 return What is the complexity of this algorithm?

ISE 407 Lecture 19 9 Depth-first Search in Directed Graphs DFS in a directed graph is very similar to DFS in an undirected graph. The main difference is that each arc is only encountered once during the search. Also, note that the notion of a component is different here. def DFSRecursion(G, v, pred): G.set_node_attr(v, color, green ) for n in G.get_neighbors(v): if G.get_node_attr(n, color ) == red pred[n] = v DFS(G, n, pred) return def DFS(G, v) for n in G.get_node_list(): G.set_node_attr(n, color, red ) DFSRecursion(G, v, []) return What nodes will this search reach?

ISE 407 Lecture 19 10 Depth-first Search in Directed Graphs As with undirected graphs, DFS in directed graphs produces a search tree that is directed out from the initial node (an out tree). At the time a node n is discovered, we record v as its predecesor. The set of arcss consisting of each node and its predecessor forms a tree rooted at v. We call the arcs in the tree tree arcs. The remaining arcs can be either Back arcs: Those connecting a vertex to an ancestor Down arcs: Those connecting a vertex to a descendant Cross arcs: Those connecting a vertex to a vertex that is neither a descendant nor an ancestor.

ISE 407 Lecture 19 11 Node Order and Arc Type Also as with undirected graphs, we can order the nodes in two different ways: postorder and preorder. As before, we refer to the preorder number of a node as its discovery time and the postorder number as its finishing time. We can identify the type of an arc as follows. It is a back arc if it leads to a node with a later finishing time. Otherwise, it is a cross arc if it leads to a node with an earlier discovery time and a down arc if it leads to a node with a later discovery time.

ISE 407 Lecture 19 12 Problems Solvable With DFS (Undirected Graphs) Cycle Detection: The discovery of a back edge indicates the existence of a cycle. Simple Path Connectivity Component Labeling Spanning Forest Two-colorability, bipartiteness, odd cycle

ISE 407 Lecture 19 13 General Graph Search Depth-first search is so called because the node selected in each step is a neighbor of the node that is farthest from the root (in the tree). This is convenient because it allows a simple recursive implementation. Could we search the graph in a different order?

ISE 407 Lecture 19 14 General Graph Search Algorithm def search(self, root, q = Stack()): if display == None: display = self.display_mode if isinstance(q, Queue): addtoq = q.enqueue removefromq = q.dequeue elif isinstance(q, Stack): addtoq = q.push removefromq = q.pop visited = {} addtoq(root) while not q.isempty(): current = removefromq() self.process_node(current, q) for n in current.get_neighbors(): if not n in visited: visited[n] = True self.process_edge(current, n) addtoq(n)

ISE 407 Lecture 19 15 General Search Algorithm The algorithm is a template for a whole class of algorithms. If Q is a stack (LIFO), we are doing depth-first search, as before. If Q is a queue (FIFO), we are doing breadth-first search. In other cases, we will want to maintain Q as a priority queue. What problem does breadth-first search of a graph solve?

ISE 407 Lecture 19 16 Complexity of Search Algorithm The search proceeds differently depending on which element v is selected from q in each iteration. q must be ordered in some way by storing it in an appropriate data structure. If q is a queue, elements are inserted at one end and removed from the other and we get FIFO ordering. If q is a stack, elements are inserted and deleted from the the same end and we get LIFO ordering. The efficiency of the algorithm can be affected by the data structure used to maintain q, what additional steps are required in process node, and what additional steps are required in process edge.

ISE 407 Lecture 19 17 Aside: Finding a Hamiltonian Path Now let s consider finding a path connecting a given pair of vertices that also visits every other vertex in between (called a Hamiltonian path). We can easily modify our previous algorithm to do this by passing an additional parameter d to track the path length. What is the change in running time?

ISE 407 Lecture 19 18 Aside: Finding a Hamiltonian Path (code) def HPath(G, v, w = None, d = None, visited = {}) if d == None: d = G.get_node_num() if v == w: return d == 0 if w == None: w = v visited[v] = True for n in v.get_neighbors(): if not visited[n]: if SPath(G, n, w, d-1, visited): return True visited[v] = False return False

ISE 407 Lecture 19 19 Aside: Hard Problems We have just seen an example of two very similar problem, one of which is hard and one of which is easy. In fact, there is no known algorithm for finding a Hamiltonian path that takes less than an exponential number of steps. This is our first example of a problem which is easy to state, but for which no known efficient algorithm exists. Many such problems arise in graph theory and it s difficult to tell which ones are hard and which are easy. Consider the problem of finding an Euler path, which is a path between a pair of vertices that includes every edge exactly once. Does this sound like a hard problem?