COMP261 Lecture 6 Dijkstra’s Algorithm.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Breadth-First and Depth-First Search
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
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.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Minimum Spanning Trees
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using 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.
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.
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.
Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Network Flow Longest Paths.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Graphs - II CS 2110, Spring Where did David leave that book? 2.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Breadth-First Search (BFS)
COMP 261 Lecture 17 Test Revision.
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.
Graphs Representation, BFS, DFS
Articulation Points 1 of 2 (Ideas)
COMP108 Algorithmic Foundations Greedy methods
May 12th – Minimum Spanning Trees
Fundamentals, Terminology, Traversal, Algorithms
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Cse 373 May 8th – Dijkstras.
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Ellen Walker CPSC 201 Data Structures Hiram College
Depth-First Search.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Data Structures and Algorithms for Information Processing
Comp 245 Data Structures Graphs.
Graph Search Applications
Graph & BFS.
Graphs Representation, BFS, DFS
Lecture 7 All-Pairs Shortest Paths
Graphs & Graph Algorithms 2
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Search Related Algorithms
Lectures on Graph Algorithms: searching, testing and sorting
Graphs.
Trees CMSC 202, Version 5/02.
Graphs Part 2 Adjacency Matrix
Tree Searching.
CMSC 202 Trees.
Artificial Intelligence
COMP171 Depth-First Search.
CSE 373: Data Structures and Algorithms
Depth-First Search CSE 2011 Winter April 2019.
Algorithms: Design and Analysis
Depth-First Search CSE 2011 Winter April 2019.
Chapter 16 1 – Graphs Graph Categories Strong Components
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
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

Intern Opportunities in Japan 6-12 Weeks from either June or November 2017 Work with Rakuten (Japanese e-commerce company) A project / other meaningful work Gain experience of working in corporate environment in Japan Fully funded by Asia NZ Foundation and Rakuten Return air fares, accommodation, meals, transport, insurance, …

Intern Opportunities in Japan Requirements: NZ citizens / PR Complete a COMP degree by March 2019 Interest in building NZ’s capacity to engage economically with Japan Willing to participate in Foundation activities following the internship Deadline: 27 March 2017 More information http://www.asianz.org.nz/content/opportunity-rakuten-computer-science-internship-japan

Outline Check Graph Connectedness using DFS Shortest Path Finding Dijkstra’s Algorithm One to all One to one

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

Connectedness: Recursive DFS DFS of trees is easy, especially recursive version: call DFS on the root node: DFS(node): count ++ // visit node for each child of node DFS(child) Graphs are more tricky We might revisit a node 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 (e.g. 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 (e.g. 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.

Shortest paths problems VUW to airport Shortest/Fastest way?

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 unvisited node with the shortest path from the start If a node has the shortest path from the start There is NO shorter path from start to that node via other unvisited node got through this slide The path must be the shortest when the node is visited from the fringe node length1 length1 <= length2 0 <= length3 length3 start length1 <= length2 + length3 length2 another node

Dijkstra's algorithm Given: a graph with weighted edges. Initialise fringe to be a set containing start node start.pathlength ← 0 For each other node, set node.pathlength ← infinity Repeat until all nodes have been visited: Choose an unvisited node from the fringe with minimum path length (i.e. length from start to node) Record the path to the current node For each unvisited neighbour of the current node Add the neighbour to fringe Mark the current node as visited

More questions and design issues What to store for each node in the fringe itself, pathlength, path How to store the paths (or find the paths) previous node of the shortest path How to store the visited nodes flag How to represent the graph node edge

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 pathLength, node, pathFrom 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 <6,B,G> <8,J,G> <10,H,G> <14,K,G> <25,F,G> <9,J,B> <11,C,B> <13,A,B> <0,G,-> <6,B,G> <8,J,G> <10,H,G> <14,K,G> <25,F,G> <14,K,G> <25,F,G> <14,K,J> <17,C,J> <20,A,H> <13,K,C> <13,K,D> <16,E,D> <31,F,K> <31,F,K> <39,F,E> <14,K,G> <25,F,G> <13,A,B> <14,K,J> <17,C,J> <13,K,H> <20,A,H> <13,K,C> <13,K,D> <16,E,D> <14,K,G> <25,F,G> <11,C,B> <13,A,B> <12,A,J> <14,K,J> <17,C,J> <13,K,H> <20,A,H> <14,K,G> <25,F,G> <13,A,B> <12,A,J> <14,K,J> <17,C,J> <13,K,H> <20,A,H> <12,D,C> <13,K,C> <14,K,G> <25,F,G> <13,A,B> <14,K,J> <17,C,J> <13,K,H> <20,A,H> <12,D,C> <13,K,C> <10,H,G> <14,K,G> <25,F,G> <9,J,B> <11,C,B> <13,A,B> <12,A,J> <14,K,J> <17,C,J> <25,F,G> <17,C,J> <31,F,K> <39,F,E> A B C E D J H G K F 5 1 3 7 25 9 6 2 23 18 4 17 10 8 14 B <11,C,B> <12,A,J> <12,D,C> C A D J H <8,J,G> E <10,H,G> K <16,E,D> <13,K,H> F G <25,F,G> <0,G,->

Extracting Shortest Path getShortestPath(start, node) path ← (node), curr ← node; while not curr.from = start: curr ← curr.from; path ← (curr, path) <6,B,G> B <11,C,B> <12,A,J> <12,D,C> C A D J H <8,J,G> E <10,H,G> K <16,E,D> <13,K,H> G F <0,G,-> <25,F,G> Path G B C D E

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?

Summary 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