Lec 2
Lec 2
Applications
Lecture 2: Graph Search
2
Review (1)
Graph types:
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
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:
7
Review (4): Edge list
Intuition: the more we store, the more trouble when we need to updates
Time Complexity:
- 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
Important notes:
- G is no-weighted
11
Generic Search
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
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
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:
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:
32
Depth First Search
The idea of DFS: use a stack to keep
the sets of vertices and retrieval (v, w)
follows the stack
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:
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.
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:
42
Homework 1
Given the following tasks, explain which algorithms to choose between (DFS or BFS or
others)
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 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