COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Breadth-First and Depth-First Search
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs CS3240, L. grewe.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
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.
Using Search in Problem Solving
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
COMP171 Depth-First Search.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Using Search in Problem Solving
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Backtracking.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
CS261 Data Structures DFS and BFS – Edge List Representation.
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
Search Related Algorithms. Graph Code Adjacency List Representation:
Minimum Spanning Trees
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Search by partial solutions.  nodes are partial or complete states  graphs are DAGs (may be trees) source (root) is empty state sinks (leaves) are complete.
COMP261 Lecture 7 A* Search. A* search Can we do better than Dijkstra's algorithm? Yes! –want to explore more promising paths, not just shortest so far.
Lecture 3: Uninformed Search
Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Network Flow Longest Paths.
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
CSC212 Data Structure - Section AB
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Articulation Points 2 of 2 (Algorithm)
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Articulation Points 1 of 2 (Ideas)
COMP261 Lecture 6 Dijkstra’s Algorithm.
Csc 2720 Instructor: Zhuojun Duan
Data Structures and Algorithms for Information Processing
Comp 245 Data Structures Graphs.
Graphs & Graph Algorithms 2
Lectures on Graph Algorithms: searching, testing and sorting
Graphs.
COMP171 Depth-First Search.
Depth-First Search CSE 2011 Winter April 2019.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

COMP261 Lecture 6 Dijkstra’s Algorithm

Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E X I U K

Connectedness: Recursive DFS Traverse the graph from one node (eg, depth first search: DFS) count nodes as you go Check whether you got to all the nodes. DFS of trees is easy, especially recursive version: call DFS on the root node: DFS (node ): count ++ for each child of node DFS(child ) Graphs are more tricky we might revisit a node and count it multiple times!  we need to record when we visit the first time and avoid revisiting! A F N G

Connectedness: Recursive DFS Traverse the graph from one node (eg, depth first search), mark nodes as you go Check whether all the nodes have been marked. DFS, recording when visited: (mark the nodes) Initialise: count ← 0, for all nodes, node.visited ← false recDFS(start ) return (count = N) recDFS (node ): if not node.visited then count ++, node.visited ← true, for each neighbour of node if not neighbour.visited recDFS(neighbour )

Connectedness: Recursive DFS Traverse the graph from one node (eg, depth first search), mark nodes as you go Check whether all the nodes have been marked. DFS, recording when visited: (explicit set of visited nodes) Initialise: count ← 0, for all nodes, visited ← { } recDFS(start ) return (count = N) recDFS (node ): if node not in visited then count ++, add node to visited for each neighbour of node if neighbour not in visited recDFS(neighbour )

Connectedness: DFS Using iteration and an explicit stack Fringe is a stack of nodes that have been seen, but not visited. Initialise: count ← 0, for all nodes, node.visited ← false push start onto fringe // fringe is a stack repeat until fringe is empty: node ← pop from fringe if not node.visited then node.visited ← true, count ++ for each neighbour of node if not neighbour.visited push neighbour onto fringe return (count = N)

A General Graph Search strategy: –Start a “fringe" with one node –Repeat: Choose a fringe node to visit; add its neighbours to fringe. Stack: DFS Queue: ? choice determines the algorithm and the result

A General Graph Search strategy Choices: –How is the fringe managed? stack? queue? priority queue? If priority queue, what is the priority? Can the fringe be pruned? –How are the neighbours determined? –When do you stop? DFS Connectedness: –fringe = stack. –neighbours = follow edges out of node –stop when all nodes visited. BFS connectedness: –fringe = queue. –neighbours = follow edges out of node –stop when all nodes visited.

Shortest paths problems Find the shortest path from start node to goal node "shortest path" –truncated Dijkstra's algorithm (Best-first search) –A* Find the shortest paths from start node to each other node "Single source shortest paths" –Dijkstra's algorithm Find the shortest paths between every pair of nodes "All pairs shortest paths" –Floyd-Warshall algorithm

Dijkstra's algorithm Idea: Grow the paths from the start node, always choosing the next node with the shortest path from the start A B C E D I H G J F

Dijkstra's algorithm Given: a graph with weighted edges. Initialise fringe to be a set containing start node start.pathlength ← 0 Initialise path length of all other nodes to  Repeat until visited contains all nodes: –Choose an unvisited node from the fringe with minimum path length (ie, length from start to node) –Record the path to the current node –Add unvisited neighbours of current node to fringe –Add the current node to visited

More questions and design issues What to store for each node in the fringe How to store the paths (or find the paths) How to store the visited nodes How to represent the –graph Node Edge –Search node (element of fringe)

Refining Dijkstra's algorithm Given: a weighted graph of N nodes and a start node –nodes have an adjacency list of edges –nodes have a visited flag and pathFrom and pathLength fields –fringe is a priority queue of search nodes  length, node, from  Initialise: for all nodes node.visited ← false, count ← 0 fringe.enqueue(  0, start, null  ), Repeat until fringe is empty or count = N :  costToHere, node, from  ← fringe.dequeue() If not node.visited then node.visited ← true, node.pathFrom ← from, count ++ for each edge out of node to neighbour if not neighbour.visited costToNeighbour ← costToHere + edge.weight fringe.enqueue(  costToNeighbour, neighbour, node  )

Illustrating Dijkstra's algorithm nodes: label, visited, parent, pathlength fringe nodes node, from, length ln 0 nd G fr - Solution Candidate Solution A/ B/ C/ E/ D/ J / H/ G/ K/ F/

Analysing Dijkstra's algorithm A node may be added to the fringe lots of times! What's the cost? How big could the fringe get? Finds shortest paths to every node Note: All the paths are represented as a linked list from the node back to the start node –shared list structure. To print/follow path to a node, have to reverse the path.

Shortest path start → goal To find best path to a particular node: –Can use Djikstra's algorithm and stop when we get to the goal: Initialise: for all nodes visited ← false, count ← 0 fringe.enqueue(  0, start, null  ), Repeat until fringe is empty or count = N :  length, nd, from,  ← fringe.dequeue() If not nd.visited then nd.visited ← true, nd.pathFrom ← from, nd.pathLength ← length If nd = goal then exit count ← count + 1 for each edge to neighbour out of node if neighbour.visited = false fringe.enqueue(  length+edge.weight, neighbour, nd  ) Only check when dequeued. Why?

Shortest path using Dijkstra's alg it explores lots of paths that aren't on the final path. ⇒ it is really doing a search dynamic programming it never revisits nodes it builds on optimal solutions to partial problems