Paths in Graphs - PowerPoint PPT Presentation

Loading...

PPT – Paths in Graphs PowerPoint presentation | free to download - id: 691502-ZGE2Y



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Paths in Graphs

Description:

Paths in Graphs Oscar Miguel Alonso M Outline The problem to be solved Breadth first search Dijkstra's Algorithm Bellman-Ford Algorithm Shortest path in DAGs ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 38
Provided by: disUnalE4
Category:
Tags: data | graphs | paths | structure | tree

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Paths in Graphs


1
  • Paths in Graphs
  • Oscar Miguel Alonso M

2
Outline
  • The problem to be solved
  • Breadth first search
  • Dijkstra's Algorithm
  • Bellman-Ford Algorithm
  • Shortest path in DAGs
  • Efficient implementation of Dijkstra (Heap)
  • Bonus graph diameter, all to all shortest path
    (Floyd algorithm)

3
Paths in graphs
  • Find the shortest path between two nodes
  • Are all the edges of the same cost?
  • Are there edges with negative cost?
  • Is the graph directed?
  • Are there cycles of negative cost?
  • Given a graph, which are the most distant nodes?

4
Breadth first search
  • Idea
  • You begin with one node S
  • Then, visit all the nodes at distance 1 from S
  • Then, visit the all nodes at distance 2 from S
  • You continue until you meet your target node or
    you have reached all the nodes

5
Breadth first search
s
6
Breadth first search
s
1
1
1
7
Breadth first search
s
1
2
1
1
8
Breadth first search
  • BFS(G,s)
  • Input Graph G(V,E), node s
  • Output array dist // distu is the
    distance from s to u
  • Vars Queue Q
  • for-each u in V
  • distu8
  • dist s0
  • Q.add(s)
  • while(!Q.empty())
  • u Q.get()
  • for-each edge (u,v) in E
  • if distv8
  • Q.add(v)
  • distvdistu1

O(E)
9
Breadth first search
0
s
2
3
1
4
Application of the algorithm see whiteboard
10
Dijkstra's algorithm
  • Edge Relaxation
  • if(distu gt distkWku)
  • distu distkWku
  • Used in weighted graphs
  • Do not use it if there are negative weighs!

11
Dijkstra's algorithm
  • Idea
  • Keep a list of nodes which you already know the
    shortest path to
  • Such list begin with only one node (s), and you
    add one node in each iteration of the algorithm
  • The node added is connected to one node in the
    list, and is the one with minimum distance to the
    origin from all candidates

12
Dijkstra's algorithm
0
0
3
1
2
1
3
2
3
3
3
1
4
13
Dijkstra's algorithm
0
0
3
1
2
1
3
2
3
3
2
3
1
4
14
Dijkstra's algorithm
0
0
3
1
3
2
1
3
2
3
3
2
3
1
4
15
Dijkstra's algorithm
0
0
3
1
3
2
1
3
2
4
3
3
2
3
1
3
4
16
Dijkstra's algorithm
  • For the implementation
  • Keep an array indicating the minimum distance so
    far to each node (initialize with infinite)
  • Update the distance each time you add a node
    (relax the edges from that node)
  • Keep a list of the nodes not reached, instead of
    the ones reached
  • Now, instead of searching the node with minimum
    distance, just pick the one with lower distance

17
Dijkstra's algorithm
0
8
3
1
8
2
1
3
2
8
3
3
8
3
1
8
4
18
Dijkstra's algorithm
0
0
3
1
3
2
1
3
2
8
3
3
2
3
1
3
4
19
Dijkstra's algorithm
0
0
3
1
3
2
1
3
2
5
3
3
2
3
1
3
4
20
Dijkstra's algorithm
0
0
3
1
3
2
1
3
2
4
3
3
2
3
1
3
4
21
Dijkstra's algorithm
0
0
3
1
3
2
1
3
2
4
3
3
2
3
1
3
4
22
Dijkstra's algorithm
0
0
3
1
3
2
1
3
2
4
3
3
2
3
1
3
4
23
Dijkstra's algorithm
  • DIJKSTRA(G,s)
  • for-each i in V
  • di ? 8
  • Q.add(i)
  • ds ? 0
  • while not empty(queue) do
  • u Q.extract_min() // take node with
    minimum dist
  • for-each v where (u, v) in E
  • if dv gt du wuv then
  • dv du wuv
  • Note that dv represents the known shortest
    distance of v from s during the process and may
    not be the real shortest distance until v is
    marked black.

24
(No Transcript)
25
Bellman-Ford algorithm
  • Useful when there are negative-weighted edges
  • Idea relax all the edges n times
  • A path can be found with max n-i edge relaxations
  • If after that, the graph allow other edge
    relaxation, the graph has a negative weight cycle

5
-3
-3
26
Bellman-Ford algorithm
Do n-1 times for-each (u, v) in E if
dv gt du wuv then dv ? du
wuv Complexity O(VE)
27
Shortest path in DAGs
  • In DAGs the problem is easier
  • Idea sort the nodes (topological sort), and
    traverse the nodes, relaxing all their edges
  • ShortestPath-DAG(G,s)
  • for-each i in V
  • di ? 8
  • ds ? 0
  • TS TopologicalSort(G)
  • for-each u in TS
  • for-each v where (u, v) is and edge
  • if dv gt du wuv then
  • dv du wuv

O(E)
28
Shortest path in DAGs
29
(No Transcript)
30
(No Transcript)
31
Heap data structure
Example extract-min
32
Heap data structure
33
Heap data structure
34
Heap data structure
35
Warshall-Floyd Algorithm
  • All to all shortest path
  • dij is the minimum distance from vertex i to
    vertex j
  • the diameter of the graph is the maximum dij
    after the execution of the algorithm

36
Warshall-Floyd Algorithm
Floyd(G) for-each (u, v) in E duv ?
wuv for-each k in V for-each i in
V for-each j in V if dij gt dik
dkj dij ? dik dkj Time
Complexity O(V3)
37
  • THANK YOU !!
About PowerShow.com