Graph Algorithms - Terminologies: Outlines: Graphs Part-1
Graph Algorithms - Terminologies: Outlines: Graphs Part-1
PART-1
1
Applications: Communication Network
2
3
8
1 10
4
5
9
11
6
7
vertex = city,
edge weight = driving distance/time
2
4 3
8 8
1 6 10
2 4 5
4 4 3
5
9
11
5 6
6 7
7
2
Street Map
2
3
8
1 10
4
5
9
11
6
7
Computer Networks
cs.brown.edu
Computer networks
Local Area Network (LAN)
Internet brown.edu
Web qwest.net
att.net
cox.net
John
Paul
David
3
Graph Terminology
A graph G = (V, E)
V = set of vertices (nodes or points)
E = set of edges (arcs or lines) = subset of V V
Thus |E| |V|2 = O(|V|2)
In an undirected graph:
edge(u, v) = edge(v, u)
In a directed graph:
edge(u,v) goes from vertex u to vertex v, notated
uv
edge(u, v) is NOT the same as edge(v, u)
Graph Terminology
A A
B C B C
D D
V = {A, B, C, D} V = {A, B, C, D}
E = {(A,B), (A,C), (A,D), (C,B)} E = {(A,B), (A,C), (A,D), (C,B),
(B,A), (C,A), (D,A), (B,C)}
4
Graph Terminology
d e d e
Graph Terminology
A Path in a graph from u to v is a sequence of edges
between vertices w0, w1, …, wk, such that (wi, wi+1) E, u =
w0 and v = wk, for 0 i k
The length of the path is k, the number of edges on the path
a b a b
c c
d e d e
10
5
Graph Terminology
Loops
If the graph contains an edge (v, v) from a vertex to itself,
then the path v, v is sometimes referred to as a loop.
a b
d e
a b
11
Graph Terminology
12
6
Graph Terminology
Path
sequence of alternating vertices
and edges
begins with a vertex
ends with a vertex
V
each edge is preceded and a b
followed by its endpoints P1
Simple path
U d X Z
path such that all its vertices and P2 h
edges are distinct. c e
Examples
P1 = (V, X, Z) is a simple path.
W g
P2 = (U, W, X, Y, W, V) is a path
that is not simple.
f
Y
13
Graph Terminology
Cycles
A cycle in a directed graph is a path of length at least 2
such that the first vertex on the path is the same as the last
one; if the path is simple, then the cycle is a simple cycle.
a b
abeda is a simple cycle.
c abeceda is a cycle, but is NOT a simple cycle.
abedc is NOT a cycle.
d e
A cycle in a undirected graph
A path of length at least 3 such that the first vertex on the path is
the same as the last one.
The edges on the path are distinct.
a b aba is NOT a cycle.
abedceda is NOT a cycle.
c abedcea is a cycle, but NOT simple.
abea is a simple cycle.
d e
14
7
Graph Terminology
Cycle
circular sequence of alternating
vertices and edges
each edge is preceded and
followed by its endpoints V
Simple cycle a b
cycle such that all its vertices and
edges are distinct U d X Z
Examples C2 h
C1 = (V, X, Y, W, U, V) is a
c e C1
simple cycle W g
C2 = (U, W, X, Y, W, V, U) is a
cycle that is not simple
f
Y
15
Graph Terminology
If each edge in the graph carries a value, then the graph is
called weighted graph.
A weighted graph is a graph G = (V, E, W), where each
edge, e E is assigned a real valued weight, W(e).
A complete graph is a graph with an edge between every
pair of vertices.
A graph is called complete graph if every vertex is
adjacent to every other vertex.
16
8
Graph Terminology
17
Graph Terminology
b c b b
2
G is a sub-graph Not a sub-graph
since, Es = {1,4,6} Vs Vs =
{1,4}
18
9
Graph Terminology
connected graph: any two
vertices are connected by
some path
An undirected graph is
connected, if for every
pair of vertices u and v
there is a path from u to v.
connected component:
maximal connected subgraph.
E.g., the graph below has 3
connected components.
19
Graph Terminology
20
10
Graph Terminology
21
Graph Terminology
22
11
Graph Terminology
End vertices (or endpoints) of an edge
U and V are the endpoints of a
Edges incident on a vertex
a, d, and b are incident on V
Adjacent vertices V
a b
U and V are adjacent h j
Degree of a vertex
U d X Z
X has degree 5
Parallel edges c e i
h and i are parallel edges
W g
Self-loop
j is a self-loop f
Y
23
Number of Edges
Directed Graph
Since edge(u, v) is not the same as edge(v, u), the number
of edges in a complete directed graph is n(n-1)
Number of edges in a directed graph is n(n-1)
24
12
Vertex Degree
2
3
8
1 10
4
5
9
11
6
7
25
8
10
9
11
26
13
In-Degree of a Vertex (Digraph)
In-degree is number of incoming edges
indegree(2) = 1, indegree(8) = 0
2
3
8
1 10
4
5
9
11
6
7
27
2
3
8
1 10
4
5
9
11
6
7
28
14
Sum of In- and Out-Degrees (Digraph)
Each edge contributes 1 to the in-degree of some
vertex and 1 to the out-degree of some other
vertex.
29
Graphs
30
15
Graph Representation
Adjacency Matrix
Adjacency Lists
31
Adjacency Matrix
Assume V = {1, 2, …, n}
An adjacency matrix represents the graph as a
n n matrix A:
A[i, j] = 1 if edge(i, j) E (or weight of edge)
= 0 if edge(i, j) E
32
16
Adjacency Matrix
Example:
A 1 2 3 4
1 1
a
d 2
2 4
b c 3
??
3 4
33
Adjacency Matrix
Example:
A 1 2 3 4
1 1 0 1 1 0
a
d 2 0 0 1 0
2 4
b c 3 0 0 0 0
3 4 0 0 1 0
34
17
Adjacency Matrix
1 2 3 4 5
2
3 1 0 1 0 1 0
2 1 0 0 0 1
1
3 0 0 0 0 1
4 4 1 0 0 0 1
5
5 0 1 1 1 0
35
Adjacency Matrix
1 2 3 4 5
2
3 1 0 1 0 1 0
2 1 0 0 0 1
1
3 0 0 0 0 1
4 4 1 0 0 0 1
5
5 0 1 1 1 0
• Diagonal entries are zero
• Adjacency matrix of an undirected graph is symmetric
A(i, j) = A(j, i) for all i and j
36
18
Adjacency Matrix
1 2 3 4 5
2
3 1 0 0 0 1 0
2 1 0 0 0 1
1
3 0 0 0 0 0
4 4 0 0 0 0 1
5
5 0 1 1 0 0
• Diagonal entries are zero
• Adjacency matrix of a digraph need not be symmetric
37
Adjacency Matrix
How much storage does the adjacency matrix
require?
Answer: O(|V|2)
38
19
Adjacency Matrix
39
Adjacency List
Adjacency list: for each vertex v V, store a list of vertices adjacent to v.
Adjacency list for vertex i is a linear list of vertices adjacent from vertex i
Each adjacency list is a chain.
2
2 aList[1] 4
3
[2] 1 5
1 [3] 5
[4] 5 1
4
5
aList[5] 2 4 3
40
20
Adjacency List
41
Adjacency List
42
21