0% found this document useful (0 votes)
48 views46 pages

Lec 2

This document discusses graph search algorithms breadth-first search (BFS) and depth-first search (DFS). It reviews graph concepts and representations. It then introduces BFS and DFS, providing pseudocode and examples. It discusses applications of each such as finding the shortest path, connected components, bridges, and strong components. Finally, it provides homework problems involving choosing the appropriate search algorithm and applying BFS and DFS to graph problems.

Uploaded by

Hà Vân
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views46 pages

Lec 2

This document discusses graph search algorithms breadth-first search (BFS) and depth-first search (DFS). It reviews graph concepts and representations. It then introduces BFS and DFS, providing pseudocode and examples. It discusses applications of each such as finding the shortest path, connected components, bridges, and strong components. Finally, it provides homework problems involving choosing the appropriate search algorithm and applying BFS and DFS to graph problems.

Uploaded by

Hà Vân
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Graph Theory and

Applications
Lecture 2: Graph Search

Ta Viet Cuong, Ph.D


HMI laboratory, FIT-UET
Today
I. Review
II. BFS and its applications
III. DFS and its applications
Homework

2
Review (1)
Graph types:

1. Direct and Undirected Graph


2. Weighted and No-weighted Graph
3. Sparse vs Dense graph: O(NlogN) and O(N2/logN)

Graph representations:

1. Adjacency Matrix
2. Adjacency List
3. Edge Lists

3
Graph structures

4
Introduce to networkx
A “high-productivity software for complex networks” analysis

- Data structures for representing various networks (directed, undirected,


multigraphs)

5
Review (3)
Quiz 1. Could you draw the adjacency matriz the Seven Bridges of Königsberg graph?

6
Review (3)
Prove or disapprove following statements:
1. Given a connected, simple, undirected graph, we have:

2. is even, for any G without loop-edge

3. Could you verify that the smallest eigenvalues of A is zero if G is connected?

7
Review (4): Edge list
Intuition: the more we store, the more trouble when we need to updates

Let keep only a list of E:

Memory Complexity: O(M)

Time Complexity:

- Verify that A, B connected to each other?


- Compute the degree of A?
- Delete/Add an edge ?
- Print the list neighbours of A?
- Print the list neighbours of neighbours of A ?
- Print the list of vertices could reach A ? 8
Review (5)
The most standard config:

- Dense Graph
- Adjacency List

9
Today
I. Review
II. BFS and its applications
III. DFS and its applications
Homework

10
Graph Search
Given a graph G = <V, E>. Answer 2 questions

- Can we go from u to v

- Length of the shorted path from u to v


- Can we go to all of v in V from u

Important notes:

- G is no-weighted

- G could be undirected or directed

11
Generic Search

Quiz 1. Could you verify the correctness of the above pseudocode?


12
Generic Search
In practice:

- How to choose the next (v, w)


- V is growing as power of depth
- The cost to store v and check
whether v is visited should be taken
into account
- Allow Repeat or
- Not Allow Repeat

13
Breadth-first search (BFS)
The idea of BFS: use a queue to keep the sets of vertices and retrieval (v, w) follows the
queue

14
BFS pseudo-code

15
BFS example

16
BFS example

17
BFS example

18
BFS example

19
BFS example

20
BFS example

21
BFS example

22
BFS example

23
Layers in BFS

24
BFS and the shortest path
We could find the path length by
marking the updated layers

Quiz: prove the correctness of the


pseudo-code

25
BFS examples
Given G is a N-vertices graph, we can go from
any vertices to other, how many BFS layers if

1. G is a line graph
2. G is a circular graph (Cn)
3. G in general

26
BFS and the number of connected component
One of the main applications of Graph Search is to find the number of connected
components

- Each run of BFS would cover a connected component

27
Complexity - Time and Memory
Analysis the complexity of the BFS on the graph given:

1. G is dense graph
2. G is sparse graph
3. G is a chess-generated graph (b ~ 35)

28
BFS in practice
Several notes on BFS:

- In general, it is difficult to use


- Checking re-visited vertices is not trivial
- There are other variants such as Two-side BFS
or Distributed-BFS

29
BFS on maze
Could you identify the order of the layers i.e ranking the nodes by visited order?

30
Problems: Counting different shortest path
Given a undirected simple graph G = <V, E> and two vertices s, t. Two paths T1 and T2 are
called separated if all the edges are different to each other. Verify that there are two
separated shortest path on graphs.

Simple approach:

- Step 1: Use BFS to find the first shortest path


- Step 2: Eraser all the edges from the first path
- Step 3: Use BFS to find the second path. Then compare the length of two paths

Quiz: Analyze the complexity of the above approach.

Can this approach get the correct answer?


31
Today
I. Review
II. BFS and its applications
III. DFS and its applications
Homework

32
Depth First Search
The idea of DFS: use a stack to keep
the sets of vertices and retrieval (v, w)
follows the stack

We can use the recursive version also

33
DFS examples

34
DFS running time
- Mostly similar to BFS:
- It’s better in term of memory
- There is an alternative version which
combine BFS and DFS at the cost of
double running time

35
DFS applications vs BFS applications
DFS: BFS:

- Fast implementation - Slow implementation


- Can ignore the repeating vertices - Use when we want to know the
- Have a nice properties related to the shortest path
connected components

36
DFS tree and its back-edge properties
Assume G=<V, E> is an undirected
graph, draw its DFS tree and the edges
outside of the trees

37
DFS and its back-edge properties
Assume G=<V, E> is an undirected simple graph, given a DFS Tree T, we define:

- A “back edge” is an edge connect from a vertex to its ancestor in the DFS tree

Theorem: By excluding the DFS Tree T, all of the other edges in E are back-edges

Proof:

38
Quiz: Draw the Backedges
Going with the alphabetical order, draw the DFS tree and its back-edges

39
Application 1: finding bridges
Given a undirected/simple graph G = <V, E>, and an edge e is call a bridge if remove e,
the number of connected components of G increase.

If there is no such e, G is 2-edge connectivity


40
Application 1: finding bridges
Hint: check if (u, v) is a bridge, and u is visited 1

before v:
0
1. (u, v) is an edge of the DFS tree T
5
2. All of the back-edges in the subtree of v
remain within the subtree of v
2

4
41
More applications
Related to the usage of DFS:

- Finding Cut-Vertex and Biconnected-graph


- Finding Strongly Connected Component in Directed Graph
- Finding topology-sort in DAG

42
Homework 1
Given the following tasks, explain which algorithms to choose between (DFS or BFS or
others)

1. Find the order of learning subjects between each semester


2. It’s heavy raining, you want to go to school
3. Playing chess

43
Homework 2a
How many connected components of the following graphs. Given each connected
components, draw its BFS tree, expanding follow the alphabetical order

44
Homework 2b
Draw the DFS tree with the backedges

45
Homework 3:
You are asked to find the bridges of a graph G = <V, E> given it is a dynamic graph:

There three operators in total:

1. Add v: add a vertex with name v


2. Add u v: add an edge between u and v
3. Query: return how many bridges in the graph

There is a total N vertices and M edges, the graph is simple and undirected, all of the
operations are valid. Find an feasible algorithm and analyzing its complexity.

46

You might also like