You are on page 1of 16

in

Graph Representation, DFS and

y.
ud
BFS

st
ty
si
er
iv
un
What is a graph?
• A set of vertices (or nodes) linked by edges

in
1

y.
3

ud
st
ty
2 5
si
er
4
iv
un

• Mathematically, we often write G = (V,E)


– V: set of vertices, so |V| = number of vertices
– E: set of edges, so |E| = number of edges
Why do we need graphs?
• To present the relationships between different

in
objects/elements in a mathematical way

y.
ud
• Examples:

st
– Friendship

ty
si
– Local Area Network (LAN)
er
– Map of a country
iv
un

• What could the vertices and edges represent


in the above examples?
Various types of graphs
• Connected/disconnected graphs

in
y.
ud
st
ty
si
er
iv
un

• The circled subgraphs are also known as


connected components
Various types of graphs
• Directed/undirected graphs

in
y.
ud
st
ty
si
er
iv
un

• You may treat each undirected edge as two


directed edges in opposite directions
Various types of graphs
• Weighted/unweighted graphs

in
y.
5

ud
0 -2

st
7

ty
si
4
er
iv
un

• You may treat unweighted edges to be


weighted edges of equal weights
How to store graphs in the program?
• Usually, the vertices are labeled beforehand

in
1 5

y.
3

ud
0 -2

st
7
2 5

ty
si
4
4
er
• 3 types of graph representations:
iv

– Adjacency matrix
un

– Adjacency list
– Edge list
Adjacency matrix
• Use a 2D array 1 5

in
0 -2 3

y.
s\t 1 2 3 4 5

ud
7
1 5 -2
2 5

st
4

ty
2 0 7 4
si
er
3
iv

4 4
un

5
Adjacency list
• N vertices, N linked lists 1 5

in
3
• Each list stores its adjacent vertices
0 -2

y.
ud
7
2 5

st
1 5|-2 3|5
4

ty
4
2 1|0 3|7
si
er
iv
3
un

4 2|4

5
Adjacency list
• Memory complexity?

in
• Time complexity for:

y.
ud
– Checking the weight of an edge between 2 given

st
nodes?

ty
si
– Querying all adjacent nodes of a given node?
er
iv
un
Depth First Search (DFS)
• Let’s review the graph, and note the color

in
changes

y.
ud
– Green: Unvisited

st
– Grey: Visited

ty
si
– Black: Dead (No more2unvisited7 adjacent nodes)
er
iv

1
un

5 3

6
4
Depth First Search (DFS)
• Advantages

in
– Useful for checking whether 2 nodes are

y.
ud
connected

st
• Drawbacks

ty
si
– Finding a shortest path using DFS is difficult
er
– Stack overflow
iv
un
Breadth First Search (BFS)
• Go to all nearest nodes first

in
• The data structure “queue” is used to store

y.
ud
the visited nodes

st
• Expand from visited (but not dead) nodes

ty
si
er
iv
un
Breadth First Search (BFS)

in
Queue 1 6 2 5 7 3 4

y.
ud
st
The path has been found!

ty
si 7
But let’s complete the BFS…
2
er
iv

1
un

5 3

6
4
Breadth First Search (BFS)
while queue Q not empty

in
dequeue the first node u from Q

y.
for each adjacent node v from u

ud
if v is unvisited

st
ty
enqueue v to Q
si
mark v as visited
er
iv
un

• Initialize all nodes as unvisited, except the


starting node
Breadth First Search (BFS)
• Advantages

in
– Shortest route is guaranteed on unweighted

y.
ud
graphs

st
• How about weighted graphs?

ty
– Avoid stack overflow
si
er
• Why don’t we always use BFS instead of DFS?
iv
un

You might also like