CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Similar documents
LECTURE 17 GRAPH TRAVERSALS

Graph: representation and traversal

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

Graph Representation

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

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

CSE 2331/5331. Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort CSE 2331/5331

Graphs & Digraphs Tuesday, November 06, 2007

Graph Representations and Traversal

Figure 1: A directed graph.

Algorithm Design and Analysis

Practice Problems for the Final

Breadth First Search. cse2011 section 13.3 of textbook

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

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

Graph: representation and traversal

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Introduction to Graphs. common/notes/ppt/

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

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

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

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

LECTURE 11 TREE TRAVERSALS

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

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

CS 310 Advanced Data Structures and Algorithms

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

Chapter 9 Graph Algorithms

CS 3410 Ch 14 Graphs and Paths

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

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

CS 206 Introduction to Computer Science II

SELF-BALANCING SEARCH TREES. Chapter 11

Design and Analysis of Algorithms

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Data Structures and Algorithms. Chapter 7. Graphs

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

Outline. Graphs. Divide and Conquer.

Algorithm Design and Analysis

Understand graph terminology Implement graphs using

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Lecture 3: Graphs and flows

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

Graph. Vertex. edge. Directed Graph. Undirected Graph

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

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

TIE Graph algorithms

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

Course Review for Finals. Cpt S 223 Fall 2008

Lecture 26: Graphs: Traversal (Part 1)

22 Elementary Graph Algorithms. There are two standard ways to represent a

CSE 100 Minimum Spanning Trees Prim s and Kruskal

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

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

CSci 231 Final Review

CS61BL. Lecture 5: Graphs Sorting

Minimum spanning trees

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

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

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

Graduate Analysis of Algorithms Prof. Karen Daniels

CS 361 Data Structures & Algs Lecture 11. Prof. Tom Hayes University of New Mexico

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Data Structures and Algorithms. Werner Nutt

The Shortest Path Problem

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

Lecture 6 Basic Graph Algorithms

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

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

Unweighted Graphs & Algorithms

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

Lecture 8: PATHS, CYCLES AND CONNECTEDNESS

Graphs. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. What is a Graph? b d f h j

Basic Graph Definitions

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

Announcements. HW3 is graded. Average is 81%

Course Review. Cpt S 223 Fall 2009

Homework Assignment #3 Graph

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Announcements Problem Set 4 is out!

ECE 250 Algorithms and Data Structures

Solutions to relevant spring 2000 exam problems

Algorithm Design and Analysis

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

CSI 604 Elementary Graph Algorithms

Module 5 Graph Algorithms

lecture29: Shortest Path Algorithms

Chapter 9 Graph Algorithms

1. Graph and Representation

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

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

TIE Graph algorithms

Shortest path problems

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

Chapter 9 Graph Algorithms

CSE 100: GRAPH ALGORITHMS

Graphs. Computer Science E-22 Harvard Extension School David G. Sullivan, Ph.D. What is a Graph? b d f h j

22 Elementary Graph Algorithms. There are two standard ways to represent a

Week 11: Minimum Spanning trees

Transcription:

CSC 172 Data Structures and Algorithms Lecture 24 Fall 2017

ANALYSIS OF DIJKSTRA S ALGORITHM CSC 172, Fall 2017

Implementation and analysis The initialization requires Q( V ) memory and run time We iterate V 1 times, each time finding next closest vertex to the source Iterating through the table requires is Q( V ) time Each time we find a vertex, we must check all of its neighbors With an adjacency matrix, the run time is Q( V ( V + V )) = Q( V 2 ) With an adjacency list, the run time is Q( V 2 + E ) = Q( V 2 ) as E = O( V 2 ) Can we do better? Recall, we only need the closest vertex How about a priority queue?

Pseudocode (Using PQ) Source: http://www.cs.dartmouth.edu/~thc/cs10/lectures/0509/0509.html CSC 172, Fall 2017

Implementation and analysis The initialization still requires Q( V ) memory and run time The priority queue will also requires O( V ) memory We must use an adjacency list, not an adjacency matrix We iterate V -1 times, each time finding the closest vertex to the source Place the distances into a priority queue The size of the priority queue is O( V ) Thus, the work required for this is O( V ln( V )) Is this all the work that is necessary? Recall that each edge visited may result in updating the priority queue Thus, the work required for this is O( E ln( V )) Thus, the total run time is O( V ln( V ) + E ln( V )) = O( E ln( V ))

Summary We have seen an algorithm for finding single-source shortest paths Start with the initial vertex Continue finding the next vertex that is closest Dijkstra s algorithm always finds the next closest vertex It solves the problem in O( E ln( V )) time using Priority Queue The algorithm can be further improved to run in time O(( V ln( V ) + E ) --- (using Fibonacci Heap; We won t cover it)

BACK TO GRAPH BASICS CSC 172, Fall 2017

Trees A graph is a tree if it is connected and there is a unique path between any two vertices Three trees on the same eight vertices Consequences: The number of edges is E = V 1 The graph is acyclic, that is, it does not contain any cycles Adding one more edge must create a cycle Removing any one edge creates two disjoint non-empty subgraphs

Trees Any tree can be converted into a rooted tree by: Choosing any vertex to be the root Defining its neighboring vertices as its children and then recursively defining: All neighboring vertices other than that one designated its parent are now defined to be that vertices children Given this tree, here are three rooted trees (out of many others) associated with it

Forests A forest is any graph that has no cycles Consequences: The number of edges is E < V The number of trees is V E Removing any one edge adds one more tree to the forest Here is a forest with 22 vertices and 18 edges There are four trees

Directed graphs In a directed graph, the edges on a graph are be associated with a direction Edges are ordered pairs (v j, v k ) denoting a connection from v j to v k The edge (v j, v k ) is different from the edge (v k, v j )

Directed graphs Given our graph of nine vertices V = {v 1, v 2, v 9 } These six pairs (v j, v k ) are directed edges E = {(v 1, v 2 ), (v 3, v 5 ), (v 5, v 3 ), (v 6, v 9 ), (v 8, v 4 ), (v 9, v 4 )}

Directed graphs The maximum number of directed edges in a directed graph is ( V -1) æv ö V E 2ç = 2 = V V - 1 = O V è 2 ø 2 ( ) ( 2 )

Directed acyclic graphs A directed acyclic graph is a directed graph which has no cycles These are commonly referred to as DAGs They are graphical representations of partial orders on a finite number of elements These two are DAGs: This directed graph is not acyclic:

GRAPH TRAVERSALS CSC 172, Fall 2017

Outline We will look at traversals of graphs Breadth-first or depth-first traversals Must avoid cycles Depth-first traversals can be recursive or iterative

Strategies Traversals of graphs are also called searches We can use either breadth-first or depth-first traversals Breadth-firstrequires a queue Depth-firstrequires a stack We each case, we will have to track which vertices have been visited requiring Q( V ) memory One option is a hash table The time complexity cannot be better than and should not be worse than Q( V + E ) Connected graphs simplify this to Q( E ) Worst case: Q( V 2 )

Breadth-first traversal Consider implementing a breadth-first traversal on a graph: Choose any vertex, mark it as visited and push it onto queue While the queue is not empty: Pop to top vertex v from the queue For each vertex adjacent to v that has not been visited: Mark it visited, and Push it onto the queue This continues until the queue is empty Note: if there are no unvisited vertices, the graph is connected,

Iterative Breadth-first traversal An implementation can use a queue Does this remind you of any other algorithm? Level order traversal

Breadth-first traversal The size of the queue is O( V ) The size depends both on: The number of edges, and The out-degree of the vertices

Depth-first traversal Consider implementing a depth-first traversal on a graph: Choose any vertex, mark it as visited From that vertex: If there is another adjacent vertex not yet visited, go to it Otherwise, go back to the most previous vertex that has not yet had all of its adjacent vertices visited and continue from there Continue until no visited vertices have unvisited adjacent vertices Two implementations: Recursive Iterative

Recursive depth-first traversal A recursive implementation uses the call stack for memory:

Iterative depth-first traversal An iterative implementation can use a stack Does this remind you of any other algorithm? Reverse Pre-order traversal

Consider this graph Example

Example Performing a breadth-first traversal Push the first vertex onto the queue A

Example Performing a breadth-first traversal Pop A and push B, C and E A B C E

Example Performing a breadth-first traversal: Pop B and push D A, B C E D

Example Performing a breadth-first traversal: Pop C and push F A, B, C E D F

Example Performing a breadth-first traversal: Pop E and push G and H A, B, C, E D F G H

Example Performing a breadth-first traversal: Pop D A, B, C, E, D F G H

Example Performing a breadth-first traversal: Pop F A, B, C, E, D, F G H

Example Performing a breadth-first traversal: Pop G and push I A, B, C, E, D, F, G H I

Example Performing a breadth-first traversal: Pop H A, B, C, E, D, F, G, H I

Example Performing a breadth-first traversal: Pop I A, B, C, E, D, F, G, H, I

Example Performing a breadth-first traversal: The queue is empty: we are finished A, B, C, E, D, F, G, H, I

Example Perform a recursive depth-first traversal on this same graph

Example Performing a recursive depth-first traversal: Visit the first node A

Example Performing a recursive depth-first traversal: A has an unvisited neighbor A, B

Example Performing a recursive depth-first traversal: B has an unvisited neighbor A, B, C

Example Performing a recursive depth-first traversal: C has an unvisited neighbor A, B, C, D

Example Performing a recursive depth-first traversal: D has no unvisited neighbors, so we return to C A, B, C, D, E

Example Performing a recursive depth-first traversal: E has an unvisited neighbor A, B, C, D, E, G

Example Performing a recursive depth-first traversal: F has an unvisited neighbor A, B, C, D, E, G, I

Example Performing a recursive depth-first traversal: H has an unvisited neighbor A, B, C, D, E, G, I, H

Example Performing a recursive depth-first traversal: We recurse back to C which has an unvisited neighbour A, B, C, D, E, G, I, H, F

Example Performing a recursive depth-first traversal: We recurse finding that no other nodes have unvisited neighbours A, B, C, D, E, G, I, H, F

Comparison Performing a recursive depth-first traversal: We recurse finding that no other nodes have unvisited neighbours A, B, C, D, E, G, I, H, F

Comparison The order in which vertices appear can differ greatly An iterative depth-first traversal may also be different again A, B, C, E, D, F, G, H, I A, B, C, D, E, G, I, H, F

Applications Applications of tree traversals include: Determining connectiveness and finding connected sub-graphs Determining the path length from one vertex to all others

Summary This topic covered graph traversals Considered breadth-first and depth-first traversals Depth-first traversals can recursive or iterative More overhead than traversals of rooted trees Considered an example with both implementations They are also called searches

CONNECTEDNESS CSC 172, Fall 2017

Outline We will use graph traversals to determine: Whether one vertex is connected to another The connected sub-graphs of a graph

Connected First, let us determine whether one vertex is connected to another v j is connected to v k if there is a path from the first to the second Strategy: Perform a breadth-first traversal starting at v j While looping, if the vertex v k ever found to be adjacent to the front of the queue, return true If the loop ends, return false

Connected Consider implementing a breadth-first traversal on a graph: Choose any vertex, mark it as visited and push it onto queue While the queue is not empty: Pop to top vertex v from the queue For each vertex adjacent to v that has not been visited: Mark it visited, and Push it onto the queue This continues until the queue is empty Note: if there are no unvisited vertices, the graph is connected,

Determining Connections Is A connected to D?

Determining Connections Vertex A is marked as visited and pushed onto the queue A A

Determining Connections Pop the head, A, and mark and push B, F and G B F G B F G

Determining Connections Pop B and mark and, in the left graph, mark and push H On the right graph, B has no unvisited adjacent vertices F G H F G

Determining Connections Popping F results in the pushing of E G H E G E

Determining Connections In either graph, G has no adjacent vertices that are unvisited H E E

Determining Connections Popping H on the left graph results in C and I being pushed C I D

Determining Connections The queue op the right is empty We determine A is not connected to D C I D

Determining Connections On the left, we pop C and return true because D is adjacent to C In the left graph, A is connected to D I

Determining Connections On the left, we pop C and return true because D is adjacent to C In the left graph, A is connected to D I

Summary This topic covered connectedness Determining if two vertices are connected

PATH LENGTH CSC 172, Fall 2017

Outline This topic looks at another problem solved by breadth-first traversals Finding all path lengths in an unweighted graph

Determining Distances Problem: find the distance from one vertex v to all other vertices Use a breadth-first traversal Vertices are added in layers The starting vertex is defined to be in the zeroeth layer, L 0 While the k th layer is not empty: All unvisited vertices adjacent to verticies in L k are added to the (k + 1) st layer Any unvisited vertices are said to be an infinite distance from Reference: Kleinberg and Tardos v

Determining Distances Consider this graph: find the distance from A to each other vertex

Determining Distances A forms the zeroeth layer, L 0 A

Determining Distances The unvisited vertices B, F and G are adjacent to A These form the first layer, L 1 B F G

Determining Distances We now begin popping L 1 vertices: pop B H is adjacent to B It is tagged L 2 F G H

Determining Distances Popping F pushes E onto the queue It is also tagged L 2 G H E

Determining Distances We pop G which has no other unvisited neighbours G is the last L 1 vertex; thus H and E form the second layer, L 2 H E

Determining Distances Popping H in L 2 adds C, I, and D to the third layer L 3 D E C I D

Determining Distances E has no more adjacent unvisited vertices Thus C, I, and D form the third layer, L 3 D C I D

Determining Distances Pop C,I, D. The queue is Empty. Stop. D

Sumary This topic found the unweighted path length from a single vertex to all other vertices A breadth-first traversal was used The first vertex is marked as layer 0 Vertices added to the queue by one in layer k are marked as layer k + 1 Later, we will see different algorithms for finding the shortest path length in weighted graphs

TOPOLOGICAL SORTING CSC 172, Fall 2017

Topological Sort In this topic, we will discuss: Motivations Review the definition of a directed acyclic graph (DAG) Describe a topological sort and applications

Motivation Given a set of tasks with dependencies, is there an order in which we can complete the tasks?

Restriction of paths in DAGs In a DAG, given two different vertices v j and v k, there cannot both be a path from v j to v k and a path from v k to v j Proof: Assume otherwise; thus there exists two paths: (v j, v 1,1, v 1,2, v 1,3,, v k ) (v k, v 2,1, v 2,2, v 2,3,, v j ) From this, we can construct the path (v j, v 1,1, v 1,2, v 1,3,, v k, v 2,1, v 2,2, v 2,3,, v j ) This a path is a cycle, but this is an acyclic graph contradiction

Definition of topological sorting A topological sorting of the vertices in a DAG is an ordering v 1, v 2, v 3,, v V such that v j appears before v k if there is a path from v j to v k

Definition of topological sorting Given this DAG, a topological sort is H, C, I, D, J, A, F, B, G, K, E, L

Example For example, there are paths from H, C, I, D and J to F, so all these must come before F in a topological sort H, C, I, D, J,A, F,B, G, K, E, L Clearly, this sorting need not be unique

Applications Consider a gentleman is getting ready for a dinner out He must wear the following: jacket, shirt, briefs, socks, tie, etc. There are certain constraints: the pants really should go on after the briefs, socks are put on before shoes

Otherwise Gentleman to Superman CSC 172, Fall 2017

Applications The following is a task graph for getting dressed: One topological sort is: briefs, pants, wallet, keys, belt, socks, shoes, shirt, tie, jacket, ipod, watch A more reasonable topological sort is: briefs, socks, pants, shirt, belt, tie, jacket, wallet, keys, ipod, watch, shoes

Topological Sort A graph is a DAG if and only if it has a topological sorting

Example On this graph, iterate the following V = 12 times Choose a vertex v that has in-degree zero Let v be the next vertex in our topological sort Remove v and all edges connected to it

Example Let s step through this algorithm with this example Which task can we start with?

Example Of Tasks C or H, choose Task C

Example Having completed Task C, which vertices have in-degree zero? C

Example Only Task H can be completed, so we choose it C

Example Having removed H, what is next? C, H

Example Both Tasks D and I have in-degree zero Let us choose Task D C, H

Example We remove Task D, and now? C, H, D

Example Both Tasks A and I have in-degree zero Let s choose Task A C, H, D

Example Having removed A, what now? C, H, D, A

Example Both Tasks B and I have in-degree zero Choose Task B C, H, D, A

Example Removing Task B, we note that Task E still has an in-degree of two Next? C, H, D, A, B

Example As only Task I has in-degree zero, we choose it C, H, D, A, B

Example Having completed Task I, what now? C, H, D, A, B, I

Example Only Task J has in-degree zero: choose it C, H, D, A, B, I

Example Having completed Task J, what now? C, H, D, A, B, I, J

Example Only Task F can be completed, so choose it C, H, D, A, B, I, J

Example What choices do we have now? C, H, D, A, B, I, J, F

Example We can perform Tasks G or K Choose Task G C, H, D, A, B, I, J, F

Example Having removed Task G from the graph, what next? C, H, D, A, B, I, J, F, G

Example Choosing between Tasks E and K, choose Task E C, H, D, A, B, I, J, F, G

Example At this point, Task K is the only one that can be run C, H, D, A, B, I, J, F, G, E

Example And now that both Tasks G and K are complete, we can complete Task L C, H, D, A, B, I, J, F, G, E, K

Example There are no more vertices left C, H, D, A, B, I, J, F, G, E, K, L

Example Thus, one possible topological sort would be: C, H, D, A, B, I, J, F, G, E, K, L

Example Note that topological sorts need not be unique: C, H, D, A, B, I, J, F, G, E, K, L H, I, J, C, D, F, G, K, L, A, B, E

Summary In this topic, we have discussed topological sorts Sorting of elements in a DAG

Acknowledgement A lot of these slides are taken from ECE 250 Algorithms and Data Structures course (University of Waterloo) offered by Prof. Douglas W. Harder. CSC 172, Fall 2017