Supp Graph 1
Supp Graph 1
Supp Graph 1
Elementary Graph
Graph Algorithms
Algorithms
Intro 1
Graphs
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(V2)
aphs-1 - 2 Lin / Intro
Devi 2
Representation of Graphs
Two standard ways.
» Adjacency Lists.
» Adjacency Matrix.
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.
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
Definition:
Forest: An acylclic graph G that may be disconnected.
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