12 - Graph
12 - Graph
2 7/26/2018
Graph Example
v1
e2 V = {v1, v2, v3}
e3
e5 E = {e1, e2, e3, e4, e5}
e1
e4 = { (v1,v2), (v1,v2),
(v1,v3), (v2,v3),
(v3,v3)
v2 v3 }
3 7/26/2018
Graph Example
V = { 1, 2, 3, 4 }
4 7/26/2018
Types of Graph
Undirected graph
– Simple Graph
– Multigraph
Directed graph
– Directed multigraph
2
Weighted graph
1 6
5 7/26/2018
Undirected Graph
The edges have no orientation
– Edge (a, b) and (b, a) are identical
Simple graph
– Undirected graph with no loop and multiple edges
Multigraph
– Undirected or directed graph that allow any multiple edges
and loops 1
2
3
4
6 7/26/2018
Directed Graph
Digraph
– D = ( V, A )
2 3 2 3
4 4
7 7/26/2018
Properties of Graph
Two vertices u and v are called adjacent if an
edge exists between them
– From example: v1 and v2 are adjacent, while v1 and v4 are not
8 7/26/2018
Properties of Graph
If an edge connects two vertices; these two
vertices are said to be incident to that edge, or,
equivalently, that edge incident to those two
vertices
– From example: e1 is incident to v1 and v2
9 7/26/2018
Properties of Graph
The set of neighbors of v is a set of vertices
adjacent to v not including v itself
– From example : neighbors of 1 = { 2, 3 }
10 7/26/2018
Properties of Graph
In digraph there are 2 more degrees to count
– In degree to count the number of head endpoints
adjacent to a node
– Out degree to count the number of tail endpoints
adjacent to a node
– From example:
d(v4) = 4
din(v4) = 2 1 1
dout(v4) = 2
2 3 2 3
4 4
11 7/26/2018
Path in Graph
sequence of edges which connect a sequence of
vertices
– by most definitions, are all distinct from one another.
12 7/26/2018
Cycle in Graph
A closed walk consists of a sequence of vertices
starting and ending at the same vertex,
– with each two consecutive vertices in the sequence
adjacent to each other in the graph
13 7/26/2018
Exercise
Boston(5) Graf G = (V, E)
1500 V = { ……………….}
Chicago(4)
E = {…………………}
1200 250
San
Fransisco 800 1000 New Write down any path
Denver(3)
(2) York(6) and circuit that exists
300
1000
in that graph!
1400 900 Write the length
1700
Los
Angeles
(1)
Write the degree of
New 1000 Miami(7) each vertex!
Orleans(8)
14 7/26/2018
Question?
Graph Representation
Adjacency matrix
Incidence matrix
Adjacency list
16 7/26/2018
Adjacency Matrix
n x n matrix, n = number of vertices
– M[x,y] = 1 if x and y are adjacent
In multigraph M[x,y] = number of edges from x to y
– In undirected graph M[x,y] = M[y,x]
1 2 3 4 5 6
1 1 1 0 0 1 0
2 1 0 1 0 1 0
3 0 1 0 1 0 0
4 0 0 1 0 1 1
5 1 1 0 1 0 0
6 0 0 0 1 0 0
17 7/26/2018
Adjacency Matrix
In digraph
– M[x,y] = 1 if there is an arc from x to y
– M[x,y] may not the same as M[y,x]
1 2 3 4 5 6
1 1 1
0 0 0 1
0 0
2 1 0 1
0 0 1
0 0
3 0 1 0 1 0 0
4 0 0 1
0 0 1 1
0
5 1 1 0 1
0 0 0
6 0 0 0 1 0 0
18 7/26/2018
Adjacency Matrix
In weighted graph
– M[x,y] = value/weight of edge from x to y
– M[x,y] = ~, if x and y are not adjacent
1 2 3 4 5 6
2
1 1
3 1
2 ~
0 ~
0 1
4 ~
0
2
2 1
2 ~
0 1
7 ~
0 1
0 ~
0
7
6 3 ~
0 1
7 ~
0 1
2 ~
0 ~
0
0
4 ~
0 ~
0 1
2 ~
0 1
6 1
2
2 4
5 1
4 1
0 ~
0 1
6 ~
0 ~
0
3
6 ~
0 ~
0 ~
0 1
2 ~
0 ~
0
19 7/26/2018
Incidence Matrix
n x m matrix,
– n = number of vertices, m = number of edges
– B[i, j] = 1 if vertex i and edge j are incident
e1 e2 e3 e4
v1 1 1 1 0
v2 1 0 0 0
v3 0 1 0 1
v4 0 0 1 1
20 7/26/2018
Incidence Matrix
In digraph
– B[i, j] = 1 if the edge enters the vertex
– B[i, j] = -1 if the edge leaves the vertex
e1 e2 e3 e4
v1 1 -1
1 -1
1 0
v2 -1
1 0 0 0
v3 0 1 0 1
v4 0 0 1 -1
1
21 7/26/2018
Adjacency List
n x 1 array of linked list
– n = number of vertices
– may also be formed in multi linked list
1
2
3
4
5
6
22 7/26/2018
Adjacency List
Adjacency matrix in form of linked list
– No wasted space
1
2
3
4
5
6
23 7/26/2018
Exercise
Write the adjacency matrix, incidence matrix, and
adjacency list
a 12 b
5 9
10 8
d 15 c
24 7/26/2018
Question?
Traversal on Graph
Depth First Search
Breadth First Search
Start from 1 :
2 3
DFS : 1 2 4 8 5 6 3 7
4 5 6 7
BFS : 1 2 3 4 5 6 7 8
26 7/26/2018
Graph Searching Algorithm
Procedure graphSearch( G : graph, v : vertex )
Dictionary
C : Container
x, w : vertex
Algorithm
insert( C, v )
while ( not isEmpty( C ) ) do
x remove( x )
if( not isVisited( x ) ) then
visit( x )
for each vertex w Є Vx
if( not isVisited( w ) then insert( C, w )
27
The Container - Stack
Depth First Search
Stack
X visited
1 Container C
1
2 3
32 1
354 12
358 124
{bottom stack}
4 5 6 7 35765 1248
3576 12485
3573 124856
3577 1248563
8
357 12485637
35 12485637
3 12485637
empty 12485637
28
The Container - Queue
Breadth First Search
Queue
X visited
1 Container C
1
2 3
23 1
345 12
4567 123
4 5 6 7 5678 1234
{head}
678 12345
788 123456
888 1234567
8
88 12345678
8 12345678
empty 12345678
29
Exercise
Implement the graph searching algorithm to this
graph
30 7/26/2018
Topological Sorting
Toposort or topological ordering
linear ordering of vertices in directed acyclic graph
Example application:
– the vertices of the graph may represent tasks to be
performed,
– the edges may represent constraints that one task must
be performed before another;
– in this application, a topological ordering is just a valid
sequence for the tasks
31 7/26/2018
Topological Sorting
V1 V2
32 7/26/2018
Topological Sorting
Logic :
– List all vertices, sort by degree of incoming edges
(indegree)
– If a vertex v has no incoming edges
Print vertex v
Remove vertex v
– Repeat until no vertex remains
Or no more vertex with 0 in degree (cycle graph)
33 7/26/2018
Topological Sorting
Calculate the inDegree
1 2 3 4 5 6 7
V1 V2 1 1 1 1
2 1 1
3 1
V3 V4 V5 4 1 1 1
5 1 1
6
V6 V7
7 1
1 2 3 4 5 6 7
0 1 2 3 1 3 2
34 7/26/2018
Topological Sorting
Dictionary
v : vertex
Algorithm
for counter 1 to nVertex do
v getNewVertexZeroIndegree( G )
if ( v = Null ) then
output(‘graph has a cycle’)
else
result[ counter ] v
visit( v )
for ( each w adjacent to v ) do
indegree[w] indegree[w] - 1
35 7/26/2018
Topological Sort
1 2 3 4 5 6 7
1 1 1 1
2 1 1
3 1
4 1 1 1
5 1 1 Counter 1 2 3 4 5 6 7
6 i v1 0 x
7 1
n v2 1 0 x
d v3 2 1 1 1 0 x
e
g
v4 3 2 1 0 x
r v5 1 1 0 x
e v6 3 3 3 3 2 1 0
e v7 2 2 2 1 0 0 x
zeroInDegree v1 v2 v5 v4 v3 v7 v6
36 7/26/2018
Exercise
Write the possible topological sort of these graphs
37 7/26/2018
THANK YOU
7/26/2018
38