Graph 1
Graph 1
Elementary Graph
Graph Algorithms
Algorithms
Graphs
Graph G = (V, E)
» V = set of vertices
» E = set of edges (VV)
Types of graphs
» Undirected: edge (u, v) = (v, u); for all v, (v, v) E (No self
loops.)
» Directed: (u, v) is edge from u to v, denoted as u v. Self loops
are allowed.
» Weighted: each edge has an associated weight, given by a weight
function w : E R.
» Dense: |E| |V|2.
» Sparse: |E| << |V|2.
|E| = O(|V|2)
aphs-1 - 2 Lin / Devi
Graphs
If (u, v) E, then vertex v is adjacent to vertex u.
Adjacency relationship is:
» Symmetric if G is undirected.
» Not necessarily so if G is directed.
If G is connected:
» There is a path between every pair of vertices.
» |E| |V| – 1.
» Furthermore, if |E| = |V| – 1, then G is a tree.
b a c
c d c d a b
d a c
» Adjacency Matrix.
1 2 1 2 3 4
a b
1 0 1 1 1
2 1 0 1 0
c d 3 1 1 0 1
3 4 4 1 0 1 0
b c
d
If weighted, store weights also in
c d c
adjacency lists.
d
a b a b d c
b a c
c d c d a b
d a c
r s t u
0
v w x y
Q: s
0
r s t u
1 0
1
v w x y
Q: w r
1 1
r s t u
1 0 2
1 2
v w x y
Q: r t x
1 2 2
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
2 2 2
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
2 2 3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
2 3 3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
3 3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q:
r s t u
1 0 2 3
2 1 2 3
v w x y
BF Tree
u v w
1/
x y z
u v w
1/ 2/
x y z
u v w
1/ 2/
3/
x y z
u v w
1/ 2/
4/ 3/
x y z
u v w
1/ 2/
B
4/ 3/
x y z
u v w
1/ 2/
B
4/5 3/
x y z
u v w
1/ 2/
B
4/5 3/6
x y z
u v w
1/ 2/7
B
4/5 3/6
x y z
u v w
1/ 2/7
F B
4/5 3/6
x y z
u v w
1/8 2/7
F B
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B C
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/12
F B C