Академический Документы
Профессиональный Документы
Культура Документы
Graph Terminology and Special Types of Graphs, Representing Graphs and Graph
Isomorphism, Connectivity, Euler and Hamilton Paths, the handshaking lemma, Single
source shortest path- Dijkstra's Algorithm, Planar Graphs, Graph Coloring.
In graph above
v4 and v5 are end vertices of e5, e4 and e5 are parallel,
e3 is a loop, The graph is not simple, e1 and e2 are adjacent, v1 and v2 are
adjacent, The degree of v1 is 1 so it is a pendant vertex, e1 is a pendant
edge, The degree of v5 is 5, The degree of v4 is 2, The degree of v3 is 0 so it
is an isolated vertex.
Graph terminology (cont.)
What is the number of edges in a complete
directed graph with N vertices?
N * (N-1)
2
O( N )
Graph terminology (cont.)
What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
2
O( N )
Directed vs. undirected graphs
618 3
2
1 2273 211
190
318
4
344 1987
2145 5
2462
Weights
6
Undirected Edges
Graph Representation
Two popular computer representations of a graph. Both
represent the vertex set and the edge set, but in different
ways.
1. Adjacency Matrix: Most useful when information about
edges is more desirable than information about vertices.
Use a 2D matrix to represent the graph
v u w
u
v 0 1 1
u 1 0 1
v w
w 1 1 0
Representation- Adjacency Matrix
Example: directed Graph G (V, E)
v u w
u
v 0 1 0
u 0 0 1
v w
w 1 0 0
Graph implementation
Array-based implementation
A 1D array is used to represent the vertices
A 2D array (adjacency matrix) is used to represent
the edges
Array-based implementation
Adjacency Matrix Example
0 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 1 0
8
1 0 0 1 1 0 0 0 1 0 1
2 9 2 0 1 0 0 1 0 0 0 1 0
1 3 0 1 0 0 1 1 0 0 0 0
4 0 0 1 1 0 0 0 0 0 0
3 7
6 5 0 0 0 1 0 0 1 0 0 0
4 6 0 0 0 0 0 1 0 1 0 0
5
7 0 1 0 0 0 0 1 0 0 0
8 1 0 1 0 0 0 0 0 0 1
9 0 1 0 0 0 0 0 0 1 0
Representation- Adjacency List
Each node (vertex) has a list of which nodes (vertex) it is adjacent
u
node Adjacency List
u v,w
v w, u
v w
w u,v
Graph implementation (cont.)
Linked-list implementation
A 1D array is used to represent the vertices
A list is used for each vertex v which contains the
vertices which are adjacent from v (adjacency list)
Linked-list implementation
Adjacency List Example
0 8
0
1 2 3 7 9
8
2 1 4 8
2 9 3 1 4 5
1 4 2 3
5 3 6
3 7
6 5 7
6
4 7 1 6
5
8 0 2 9
9 1 8
Adjacency matrix vs. adjacency list representation
Adjacency matrix
Good for dense graphs --|E|~O(|V|2)
Memory requirements: O(|V| + |E| ) = O(|V|2 )
Connectivity between two vertices can be tested
quickly
Adjacency list
Good for sparse graphs -- |E|~O(|V|)
Memory requirements: O(|V| + |E|)=O(|V|)
Vertices adjacent to another vertex can be found
quickly
Paths and Circuits
A walk in a graph is an alternating sequence of adjacent vertices
and edges
A path is a walk that does not contain a repeated edge
A closed walk is a walk that starts and ends at the same vertex
them.
The graph is called connected when any pair of its vertices is connected
path
If two vertices are part of a circuit and one edge is removed from the circuit
Butterfly graph
Complete graph K5
Complete graph
Utility graph K3,3
K4
Simple graphs – special cases
Complete graph: Kn, is the simple graph that contains exactly one edge
between each pair of distinct vertices.
K1 K2 K3
K4
Simple graphs – special cases
C3 C4
Simple graphs – special cases
W3 K4 W4
Simple graphs – special cases
N-cubes: Qn, vertices represented by 2n bit strings of length n. Two
vertices are adjacent if and only if the bit strings that they represent differ
by exactly one bit positions
10 11
0 1
00 01
Q1 Q2 Q3
Bipartite graphs
In a simple graph G, if V can be partitioned into two disjoint sets V1 and V2 such that every edge in the graph
connects a vertex in V1 and a vertex V2 (so that no edge in G connects either two vertices in V1 or two vertices in V2)
Application example: Representing Relations
Representation example: V1 = {v1, v2, v3} and V2 = {v4, v5, v6}
v4
v1
v5
v2
v6
v3
V V2
1
Complete Bipartite graphs
Km,n is the graph that has its vertex set portioned into two subsets of m and n
vertices, respectively There is an edge between two vertices if and only if one
vertex is in the first subset and the other vertex is in the second subset.
K2,3 K3,3
Subgraphs
A subgraph of a graph G = (V, E) is a graph H =(V’, E’) where V’ is a subset of V
and E’ is a subset of E
Application example: solving sub-problems within a graph
Representation example: V = {u, v, w}, E = ({u, v}, {v, w}, {w, u}}, H 1 ,
H2
u u u
v w v w v
G H1 H2
Subgraphs
G = G1 U G2 wherein E = E1 U E2 and V = V1 U V2, G, G1 and G2 are simple graphs of G
u
u
w v
w w v
G1 G2 G
Isomorphism of Graphs
Graphs G1 and G2 are said to be isomorphic if −
Their number of components (vertices and edges) are same.
Their edge connectivity is retained.
Number of vertices of a particular degree
Possession of a circuit of a particular length
Possession of Euler circuit, Hamiltonian circuit
An isomorphism
Graph G Graph H
between G and H
f(a) = 1f(b) = 6
f(c) = 8
f(d) = 3
f(g) = 5
f(h) = 2
f(i) = 4
f(j) = 7
Graph searching
Problem: find a path between two nodes of the
graph
Methods: Depth-First-Search (DFS) or Breadth-
First-Search (BFS)
Depth-First-Search (DFS)
What is the idea behind DFS?
Travel as far as you can down a path
Back up as little as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is
no next vertex)
DFS can be implemented efficiently using a stack
LIFO Last In First Out (Books)
Breadth-First-Search (BFS)
What is the idea behind BFS?
Look at all possible paths at the same depth before
you go at a deeper level
Back up as far as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is
no next vertex)
BFS can be implemented efficiently using a queue
FIFO
Single-source shortest-path problem
There are multiple paths from a source vertex to a
destination vertex
Shortest path: the path whose total weight (i.e., sum
of edge weights) is minimum.
• Examples of the algorithms that compute these
shortest path are Dijkstra and Bellman-Ford
algorithms as well as algorithms that find the
shortest path between all pairs of nodes, e.g. Floyd-
Marshall.
Single-source shortest-path problem
(cont.)
Common algorithms: Dijkstra's algorithm,
Bellman-Ford algorithm
BFS can be used to solve the shortest graph
problem when the graph is weightless or all the
weights are the same
(mark vertices before Enqueue)
Dijkstra’s Algorithm
Assume V1 is s and Dv is the 1
V2 V3
distance from node s to node v. 4
1 2 3
5
V6
If there is no edge connecting V1 V7
4 4
two nodes x and y → w(x,y) = 3 2
∞ V4 V5
6
4 4 D6=∞ 4 4 D6=∞
3 3 2
2
V4 6 V5 V4 6 V5
D5=∞
D4=3 D4=3 D5=∞
V’ = {1} V’ = {1,2}
Contd…
1 1
D2=1 V2 V3 D3=2 D2=1 V2 V3 D3=2
1 2 3 4 1 2 3 4
5 5
V1 D7=3 V7 V6 V1 D7=3 V7 V6
4 4 D6=6 4 4 D6=6
3 2 3 2
V4 6 V5 D4=3 V4 6 V5 D =9
D4=3 D5=∞ 5
V’ = {1,2,3} V’ = {1,2,3,4}
1 1
D2=1 V2 V3 D3=2 D2=1 V2 V3 D3=2
1 2 3 4 1 2 3 4
5 5
V1 D7=3 V7 V6 V1 D7=3 V7 V6
4 4 D6=6 4 4 D6=6
3 2 3 2
V4 6 V5 V4 6 V5
D4=3 D5=7 D4=3 D5=7
V’ = {1,2,3,4,7} V’ = {1,2,3,4,7,6}
Contd…
1
D2=1 V2 V3 D3=2
The algorithm terminates
1 2 3 4
when all the nodes have
5
V1 D7=3 V7 V6 been processed and their
4 4 D6=6 shortest distance to node 1
3 2 has been computed
V4 6 V5
D4=3 D5=7
V’ = {1,2,3,4,7,6,5}
1
V2 V3
1 2 3 4
Note that the tree computed is
5
not a minimum weight V1 V7 V6
spanning tree. A MST for the 4 4
given graph is → 3 2
V4 6 V5
Bellman-Ford Algorithm
1 Until (Dvh = Dvh-1 v V ) or (h = |V |)
V2 V3
do
1 2 3 4 h = h + 1;
5 For v V do
V1 V7 V6
Dv = min{Du + w(u,v)} u V;
h+1 h
4 4
3 2 EndFor
EndUntil
V4 6 V5
h 1 2 3 4
D3 ∞ 2 2 2
5
h V1 V7 V6
D4 3 3 3 3
4 4
h ∞ 9 7 7 3 2
D5
h V4 6 V5
D6 ∞ ∞ 6 6
h ∞ 3 3 3
D
Floyd-Warshall Algorithm
D= W
V2
2
For u = 1 to |V | do
2
2 For s = 1 to |V | do
8
6 For v = 1 to |V | do
V1 V3 Ds,v = min{Ds,v , Ds,u+ Wu,v}
4
1 EndFor
EndFor
1
4
3 EndFor
1 3
V4 V5
5
V1 V2 V3 V4 V5
0 2 4 3 V1 0 2 4 3
2 0 8 1 V2
2 0 8 1
D0 6 2 0 4 3 V3 D1 6 2 0 4 3
1 0 5 V4
1 3 5 0 4
1 0 V5 1 0
Floyd-Warshall Algorithm (Example)
0 2 4 3 0 2 4 8 3
2 0 6 1 2 0 6 10 1
D2 4 2 0 4 3 D3 4 2 0 4 3
1 3 5 0 4 1 3 5 0 4
1 0 1 0
0 2 4 8 3 0 2 4 4 3
2 2 1
0 6 10 1 0 6 2
D4 4 2 0 4 3 D5 4 2 0 4 3
1 3 5 0 4 1 3 5 0 4
2 4 6 1 0 2 4 6 1 0
Distributed Asynchronous Shortest Path
Algorithms
Each node computes the path with the shortest
weight to every network node
There is no centralized computation
As for the distributed MST algorithm described in
[Gallager, Humblet, and Spiral], control messaging
is required to distributed computation
Asynchronous means here that there is no
requirement of inter-node synchronization for the
computation performed at each node of for the
exchange of messages between nodes
Distributed Bellman-Ford Algorithm Example
Initial Ds,V
1 C
B A B C D E
s
7
A 0 7 ∞ ∞ 1
A 8 2 B 7 0 1 ∞ 8
C ∞ 1 0 2 ∞
1 D ∞ ∞ 2 0 2
E D E 1 8 ∞ 2 0
2
Ds,V
1
B C
s A B C D E
7
A 0 7 ∞ ∞ 1
A 8 2 B 7 0 1 ∞ 8
C ∞ 1 0 2 ∞
1
D ∞ ∞ 2 0 2
E D E 1 8 4 2 0
2
E receives D’s routes and updates its Ds,V
Distributed Bellman-Ford Algorithm Example
a
d
e c
b
h
f g j
i
Graph Coloring Problem
Graph coloring is an assignment of "colors", almost always
taken to be consecutive integers starting from 1 without loss of
generality, to certain objects in a graph. Such objects can be
vertices, edges, faces, or a mixture of the above.
C4 C5
K4 K2, 3
Vertex Covering Problem
The Four color theorem: the chromatic number of a planar
graph is no greater than 4
Example: G1 chromatic number = 3, G2 chromatic number = 4
(Most proofs rely on case by case analysis).
G1 G2
Euler Paths and Circuits
The Seven bridges of Königsberg
C
c
D
A
a d
B
b
Euler Paths and Circuits
N
o B
.
Necessary and Sufficient Conditions
e e
d c d c c d e
yes no no
(a, e, c, d, e, b, a)
Example
Which of the following graphs has an
Euler path?
a b a b a b
e e
d c d c c d e
yes no yes
(a, e, c, d, e, b, a ) (a, c, d, e, b, d, a, b)
Euler Circuit in Directed Graphs
NO (a, g, c, b, g, e, d, f, a) NO
Euler Path in Directed Graphs
NO (a, g, c, b, g, e, d, f, a) (c, a, b, c, d, b)
Hamilton Paths and Circuits
number
number number of regions
of vertices of edges
Example of Euler’s theorem
R3 v=4,e=6,r=4, v-e+r=2