Graph Traversal and Shortest Path Algorithms, Slides of Discrete Mathematics

An overview of graph traversal algorithms, including depth-first search (dfs) and breadth-first search (bfs. It also covers the concept of shortest paths in weighted directed graphs and introduces dijkstra's algorithm as a solution to find the shortest path between two vertices.

Typology: Slides

2011/2012

Uploaded on 07/11/2012

fazi
fazi 🇵🇰

4.7

(10)

15 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Graph Traversal
Discrete Mathematics
Problems related to Graph
Graph Traversal
Topological Sort
Spanning Tree
Minimum Spanning Tree
Shortest Path
Graph Traversal Algorithm
To traverse a tree, we use tree traversal algorithms like
pre-order, in-order, and post-order to visit all the nodes
in a tree
Similarly, graph traversal algorithm tries to visit all the
nodes it can reach.
If a graph is disconnected a graph traversal that begins at
If a graph is disconnected
,
a graph traversal that begins at
a node v will visit only a subset of nodes, that is, the
connected component containing v.
Two basic Traversal Algorithms
Two basic graph traversal algorithms:
Depth-first-search (DFS)
After visit node v, DFS strategy proceeds along a path from
v as deeply into the graph as possible before backing up
Breadth-first-search (BFS)
After visit node v BFS strategy visits every node adjacent to
After visit node v
,
BFS strategy visits every node adjacent to
v before visiting any other nodes
Depth-first search (DFS)
From a given node v, it first visits itself. Then, recursively
visit its unvisited neighbours one by one.
DFS Example
•Start from v
3
v
v
3
v2
v3
1
v
2
2
v
1
v4
v
3
v5
G
2
v1
3v4
4
v5
5
x
x
x
xx
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Graph Traversal and Shortest Path Algorithms and more Slides Discrete Mathematics in PDF only on Docsity!

Graph Traversal

Discrete Mathematics

Problems related to Graph

  • Graph Traversal
  • Topological Sort
  • Spanning Tree
  • Minimum Spanning Tree
  • Shortest Path

Graph Traversal Algorithm

  • To traverse a tree, we use tree traversal algorithms like pre-order, in-order, and post-order to visit all the nodes in a tree
  • Similarly, graph traversal algorithm tries to visit all the nodes it can reach.
  • • If a graph is disconnected a graph traversal that begins atIf a graph is disconnected, a graph traversal that begins at a node v will visit only a subset of nodes, that is, the connected component containing v.

Two basic Traversal Algorithms

  • Two basic graph traversal algorithms:
    • Depth-first-search (DFS)
      • After visit node v, DFS strategy proceeds along a path from v as deeply into the graph as possible before backing up
    • Breadth-first-search (BFS)
      • After visit node v BFS strategy visits every node adjacent to• After visit node v, BFS strategy visits every node adjacent to v before visiting any other nodes

Depth-first search (DFS)

  • From a given node v, it first visits itself. Then, recursively visit its unvisited neighbours one by one.

DFS Example

  • Start from v 3

v v 3

v 2

(^1) v 3

v 1 2 v^2

v 4

v 3

v 5

G

2

v 1

3 v 4

4

v 5

5

x x^ x x (^) x

docsity.com

Non-recursive DFS example

visit stack v 3 v (^3) v 2 v 3 , v (^2) v 1 v 3 , v 2 , v (^1) backtrack v 3 , v (^2)

v 1 v 3

v 2 x x^ x 3 ,^2 v 4 v 3 , v 2 , v (^4) v 5 v 3 , v 2 , v 4 , v (^5) backtrack v 3 , v 2 , v (^4) backtrack v 3 , v (^2) backtrack v (^3) backtrack empty

v 4 v^5

G

x (^) x

Breadth-first search (BFS)

  • BFS strategy looks similar to level-order. From a given node v, it first visits itself. Then, it visits every node adjacent to v before visiting any other nodes. - 1. Visit v - 2. Visit all v’s neigbours - 3. Visit all v’s neighbours’ neighbours - …
  • Similar to level-order, BFS is based on a queue.

BFS example

  • Start from v 5

v (^5)

1

v 1 2 3

v 2 v 3

Visit Queue (front to back) v 5 v 5 empty x (^) v (^3) v v 3 v 3 4

3

v (^2)

4

v (^1)

5

1

v (^4) v 5 G

x

v 3 v 3 v 4 v 3 , v 4 v 4 v 2 v 4 , v 2 v 2 empty v 1 v 1 empty

x

x x

Shortest Paths

Department of Computer & Information Sciences Pakistan Institute of Engineering and Applied Sciences

Umar Faiz http://www.pieas.edu.pk/umarfaiz/cis

Discrete Mathematics

Shortest path

  • Consider a weighted directed graph
    • Each node x represents a city x
    • Each edge (x, y) has a number which represent the cost of traveling from city x to city y Problem:
    • Find the minimum cost to travel from city x to city y Solution:
    • Find the shortest path from x to y

Weighted Graphs

  • A weighted graph is a graph in which each edge (u, v) has a weight w(u, v).
  • Weights can represent distance, cost, time, capacity, etc.
  • The length of a path in a weighted graph is the sum of the weights on the edges.

docsity.com

Dijkstra Animated Example Dijkstra Animated Example

Dijkstra Animated Example Dijkstra Animated Example

Dijkstra Animated Example Dijkstra Animated Example

docsity.com

Dijkstra Animated Example Dijkstra Animated Example

docsity.com