Download presentation
Presentation is loading. Please wait.
1
COMP261 Lecture 6 Dijkstra’s Algorithm
2
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, …
3
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
4
Outline Check Graph Connectedness using DFS Shortest Path Finding
Dijkstra’s Algorithm One to all One to one
5
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
6
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
7
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 )
8
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 )
9
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)
10
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
11
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.
12
Shortest paths problems
VUW to airport Shortest/Fastest way?
13
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
14
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
15
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
16
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
17
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 )
18
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,->
19
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
20
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?
21
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.