Lecture 9 Graph algorithms: BFS and DFS search - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Lecture 9 Graph algorithms: BFS and DFS search

Description:

Recursively find a DFS' in graph G restricted to free nodes and node v as the ... and since it is in connected component with v it will be in DFS' part with ... – PowerPoint PPT presentation

Number of Views:1090
Avg rating:3.0/5.0
Slides: 14
Provided by: DarekKo5
Category:
Tags: bfs | dfs | algorithms | dfs | graph | lecture | search

less

Transcript and Presenter's Notes

Title: Lecture 9 Graph algorithms: BFS and DFS search


1
Lecture 9 Graph algorithmsBFS and DFS search
  • COMP 523 Advanced Algorithmic Techniques
  • Lecturer Dariusz Kowalski

2
Overview
  • Previous lectures
  • Greedy algorithms
  • Minimum spanning tree - many greedy approaches
    (Prims and Kruskals algorithms)
  • Priority Queues
  • This lecture
  • Representation of graphs
  • Breadth-First Search (BFS)
  • Depth-First Search (DFS)

3
How to represent graphs?
  • Given graph G (V,E) , how to represent it?
  • Adjacency matrix ith node is represented as ith
    row and ith column, edge between ith and jth
    nodes is represented as 1 in row i and column j,
    and vice versa (0 if there is no edge between i
    and j)
  • Adjacency list nodes are arranged as array/list,
    each node record has the list of its neighbors
    attached

1
2
3
4
1
2
3
4
1
0
1
1
1
1
2
1
3
2
1
0
1
0
3
1
2
4
4
2
3
1
1
0
1
4
1
3
4
1
0
1
0
3
Adjacency matrix
Adjacency list
4
Adjacency matrix
  • Advantages
  • Check in constant time if an edge belongs to the
    graph
  • Disadvantages
  • Representation takes memory O(n2) - versus O(m)
  • Examining all neighbors of a given node requires
    time O(n) - versus O(m/n) in average
  • Disadvantages especially for sparse graphs!

1
2
3
4
1
2
3
4
1
0
1
1
1
1
2
1
3
2
1
0
1
0
3
1
2
4
4
2
3
1
1
0
1
4
1
3
4
1
0
1
0
3
Adjacency matrix
Adjacency list
5
Adjacency list
  • Advantages
  • Representation takes memory O(mn) - versus O(n2)
  • Examining all neighbors of a given node requires
    time O(m/n) in average - versus O(n)
  • Disadvantages
  • Check if an edge belongs to the graph requires
    time O(m/n) in average - versus O(1)
  • Advantages especially for sparse graphs!

1
2
3
4
1
2
3
4
1
0
1
1
1
1
2
1
3
2
1
0
1
0
3
1
2
4
4
2
3
1
1
0
1
4
1
3
4
1
0
1
0
3
Adjacency matrix
Adjacency list
6
Breadth-First Search (BFS)
  • Given graph G (V,E) of diameter D and the root
    node
  • Goal find a spanning tree such that the
    distances between nodes and the root are the same
    as in graph G
  • Idea of the algorithm
  • For layers i 0,1,,D
  • while there is a node in layer i 1 not added to
    the partial BFS tree add the node and any edge
    connecting it with layer i

root
layer 0
layer 2
layer 1
7
Implementing BFS
  • Structures
  • Adjacency list
  • Lists L0,L1,,LD
  • Array Discovered1n
  • Algorithm
  • Set L0 root
  • For layers i 0,1,,D
  • Initialize empty list Li1
  • For each node v in Li take next edge adjacent to
    v and if its second end w is not marked as
    Discovered then add w to Li1 and v,w to
    partial BFS

root
layer 0
layer 2
layer 1
8
Analysis of BFS
  • Correctness
  • By induction on layer number each node in layer
    i is in the list Li
  • Each none w in layer i has its predecessor on the
    distance path in layer i-1,
  • consider the first of such predecessors v in list
    Li-1 w is added to list Li by
  • the step where it is considered as the neighbor
    of v
  • Complexity
  • Time O(mn) - each edge is considered at most
    twice - since it occurs twice in the adjacency
    list
  • Memory O(mn) - adjacency list takes O(mn),
    lists L0,L1,,LD have at most 2n elements in
    total, array Discovered has n elements

root
layer 0
layer 2
layer 1
9
Depth-First Search (DFS)
  • Given graph G (V,E) of diameter D and the root
    node
  • Goal find a spanning tree such that each edge in
    graph G corresponds to the ancestor relation in
    the tree
  • Recursive idea of the algorithm
  • Repeat until no neighbor of the root is free
  • Select a free neighbor v of the root and add the
    edge from root to v to partial DFS
  • Recursively find a DFS in graph G restricted to
    free nodes and node v as the root and add it to
    partial DFS

root
root
root
10
Implementing DFS
  • Structures
  • Adjacency list
  • List (stock) S
  • Array Discovered1n
  • Algorithm
  • Set S root
  • Consider the top element v in S
  • For each neighbor w of node v, if w is not
    Discovered then put w into the stock S and start
    the next iteration for w as the top element
  • Otherwise remove v from the stock, add edge v,z
    to partial DFS, where z is the current top
    element, and start the next iteration for z as
    the top element
  • Remark after considering the neighbor of node v
    we remove this neighbor from adjacency list to
  • avoid considering it many times!

root
root
root
11
Analysis of DFS
  • Correctness
  • By induction on recursive call
  • Consider edge v,w in G let v becomes first
    joint to DFS. Then w is free
  • and since it is in connected component with v it
    will be in DFS part with
  • root v, so v is the ancestor of w
  • Complexity
  • Time O(mn) - each edge is considered at most
    twice - while adding or removing from the stock
  • Memory O(mn) - adjacency list takes O(mn),
    stock S and array Discovered has at most n
    elements each

root
root
root
12
Conclusions
  • Graph representations
  • Adjacency array
  • Adjacency list
  • Searching algorithms BFS and DFS in time and
    memory O(m n)

13
Exercises
  • Prove that obtained DFS and BFS are spanning
    trees
Write a Comment
User Comments (0)
About PowerShow.com