You are on page 1of 43

What is a Graph?

A graph G = (V,E) is composed of:


V: set of vertices
E: set of edges connecting the vertices in V
An edge e = (u,v) is a pair of vertices
Example:
a b V=
{a,b,c,d,e}

c E= {(a,b),
(a,c),(a,d),
(b,e),(c,d),
d e (c,e),
(d,e)}
A graph consists G of

• a set V of elements called nodes ( points or vertices )


• a set E of edges such that each e in E is identified with unique unordered pair [u , v] of nodes in v
and is denoted by e = [u , v]

G=(V,E)

If e = [ u , v ] then

end points of e : u , v
Adjacent nodes or neighbor nodes : u , v
degree of a node : number of edges in node
isolated node : if deg(u) = 0

Path : path p of length n from node u to node v is a sequence of n+1 nodes


( v0, v1, v2, . . . . . , vn) such that u = v0 and v = vn
vi-1 is adjacent to vi for I = 1 to n
Closed Path : if v0 = vn

Simple Path : If all nodes in a path are distinct


K - Cycle : cycle of length k

Connected Graph : If there is a path between any two of its node

Complete graph : If every node u in G is adjacent to every other node v in G. A complete graph
will have n (n-1) /2 edges

Tree graph of free tree : Connected Graph without cycle

Labeled graphs : if edges are assigned data

Weights : non negative numeric value, may be Length of the edge. If no weight is specified then
w(e) = 1 is considered

Weight of the Path : Sum of weights of all edges in this path

Multiple Edges : e1 and e2 are multiple edges if both e1 and e2 = [u , v]

Loops : is it has identical end points, e = [ u , u]

Multigraph : with multiple edges and loops

Finite multigraph : with finite number of edges and nodes


Directed Graph or Digraph : if edges have direction
edge is identified by ordered pair of nodes ( u , v )
Arc : Directed Edge

Different terminology used for end points : Begin End, Origin and terminal point, predecessor and
successor

Outdegree of node u : number of edges beginning at u

Indegree of node u : number of edges ending at u

Source node: +ve outdegree and 0 indegree

Sink : +ve indegree and 0 outdegree

A node v is reachable from node u if there is a directed path from v to u

Strongly connected directed graph : if for each pair of node there is a path from u to v and v to u

Unilaterally connected directed graph : if for each pair of node there is a path from u to v or v to u
Applications
CS16
electronic circuits

networks (roads, flights,


communications)

JFK

LAX STL
HNL
DFW
FTL
Examples: 0
3
0 2
1 2
3 1 2 3 3 3
3 4 5 6
3
G1 3 1 1 1 1
G2

directed graph
0 1 in: 1, out: 2
in-degree
in:1, out: 1 out-degree
2 in: 1, out: 0
G3
Terminology:
Path
path: sequence of
3 2
vertices v1,v2,. . .vk such
that consecutive
vertices vi and vi+1 are 3
adjacent.
3 3

a b a b

c c

d e d e
abedc bedc
7
More Terminology
simple path: no repeated vertices
a b

bec
c

d e
cycle: simple path, except that the last vertex is the same as
the first vertex
a b

acda
c

d e
Even More Terminology

•connected graph: any two vertices are


connected by some path

connected not connected


subgraph: subset of vertices and edges forming a graph
connected component: maximal connected subgraph. E.g., the graph
below has 3 connected components.
Subgraphs Examples
0
0 0 1 2 0
1 2
1 2 3 1 2
3
G1 (i) (ii) (iii) 3(iv)
(a) Some of the subgraph of G1
0
0 0 0 0

1 1 1 1

2 2 2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
G3
More…

tree - connected graph without cycles


forest - collection of trees

tree

tree

forest

tree

tree
Connectivity

Let n = #vertices, and m = #edges


A complete graph: one in which all pairs of vertices
are adjacent
How many total edges in a complete graph?
Each of the n vertices is incident to n-1 edges, however, we
would have counted each edge twice! Therefore, intuitively,
m = n(n -1)/2.
Therefore, if a graph is not complete, m < n(n -1)/2

n 5
m  (5 
More Connectivity

n = #vertices
n5
m = #edges m4
For a tree m = n - 1
If m < n -
1, G is not
connected
n5
m3
Oriented (Directed) Graph
A graph where edges are directed
Directed vs. Undirected Graph
An undirected graph is one in which the pair
of vertices in a edge is unordered, (v0, v1) =
(v1,v0)
A directed graph is one in which each edge is
a directed pair of vertices, <v0, v1> != <v1,v0>

tail head
Adjacency Matrix

Let G=(V,E) be a graph with n vertices.


The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
Merits of Adjacency Matrix

From the adjacency matrix, to determine the


connection of vertices is easy
n 1

The degree of a vertex is  adj _ mat[i][ j ]


j 0

For a digraph (= directed graph), the row sum is


the out-degree, while the column sum is the in-
degree
n 1 n 1
ind (vi )   A[ j , i ] outd (vi )   A[i , j ]
j 0 j 0
Linked representation(Adjacency List)

Each row in adjacency matrix is represented as an


adjacency list.
0 0

1 2
1
3
0 1 2 3 2
1 0 2 3
0 1
2 0 1 3
1 0 2
3 0 1 2
2
G1 G2
Sequential Representation of Graphs : Adjacency Matrix
and Path Matrix
Y X

Z W

0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 1
1 0 1 1 1 0 1 2
A1 = A2 = A3 = 1 0 2 2 2 0 2 3
1 0 0 1 0 0 1 1 1 0 1 1 A4 = 1 0 1 2
0 0 1 0 1 0 0 1 0 0 1 1 1 0 1 1

Number of Paths between Y to W of length 3 are 2


(Y -> Z -> X -> W AND Y->W->Z->W)
B4 = A1 + A2 + A3 + A4
• entry of matrix B4 gives the number of paths of length 4 or less from node v i to vj

• entry of matrix Br gives the number of paths of length r or less from node v i to vj
Path matrix: Let G be simple directed graph with m
nodes (v1, v2, ....... vm ). Then, path matrix or reach-
ability matrix of G is:
Pij = 1 (if there is path from vi to vj)
= 0 (otherwise)

B = A1 + A2 + A3 + A4

Gives path matrix P when all non- 1 0 1 1


zero entries are replaced by 1
1 0 1 1
P=
1 0 1 1

1 0 1 1
Warshall’s Algorithm
It is used to find path matrix in efficient way,
rather than calculating powers of adjacency
matrix.

First we define m-square Boolean matrices P0, P1, ......


Pm as follows. Let Pk [i, j] denote the ij entry of matrix
Pk. Then, we define:
Pk [i, j] = 1 (if there is simple path from vi to vj which use
nodes from v1, v2, ....... vk ).

= 0 (otherwise)
Warshall’s Algorithm
Warshall observed that Pk [i, j] = 1 can occur in
following 2 cases:

There is simple path from vi to vj which only used nodes from


v1, v2, ....... vk-1 , hence Pk-1 [i, j] = 1

There is simple path from vi to vk and from vk to vj which only


used nodes from v1, v2, ....... vk-1 , hence Pk-1 [i, k] = 1 and
Pk-1 [k, j] = 1

• Accordingly:
Pk [i, j] = Pk-1 [i, j] v (Pk-1 [i, k] ^ Pk-1 [k, j] )
Finding transitive closure
3
3 1
1

2 4
4
2 3 R2
R1 1 0 0 1 0
R0
0 0 1 0 0 0 1 0 1 0 1 1
1 0 0 1 1 0 1 1 0 0 0 0
4
0 0 0 0 0 0 0 0 2 1 1 1 1
0 1 0 0 0 1 0 0

3 R4
3 R 3
1
1 0 0 1 0 0 0 1 0
1 0 1 1 1 1 1 1
0 0 0 0 4 0 0 0 0
2
4 1 1 1 1 1 1 1 1
2
Warshall’s Algorithm
A directed graph G with M nodes is maintained in memory by its
adjacency matrix A. This algo. finds path matrix P of graph G.

1. Repeat for i, j = 1, 2, ....... M : //[Initializes P]


If A[i,j] = 0 then Set P[i,j] = 0
else Set P[i,j] = 1
2. Repeat Steps 3 and 4 for k = 1, 2, ...... M //[updates P]
Repeat Step 4 for i = 1, 2, ...... M
Repeat for j = 1, 2, ...........M
Set P[i,j] = P[i,j] v (P[i,k] ^ P[k,j])
3. Exit
Shortest Path (Modified Warshall’s algorithm)

7 5 ∞ ∞ 7 5 ∞ ∞

7 ∞ ∞ 2 7 12 ∞ 2
Q0 = Q1 =
∞ 3 ∞ ∞ ∞ 3 ∞ ∞

4 ∞ 1 ∞ 4 9 1 ∞

7 5 ∞ 7
7 5 8 7
7 5 ∞ 7 7 12 ∞ 2
Q3 =
6 6 3 2
7 12 ∞ 2 10 3 ∞ 5 Q4 =
Q2 =
9 3 6 5
10 3 ∞ 5 4 4 1 6
4 4 1 6
4 9 1 11
Shortest Path (Modified Warshall’s algorithm)

Graph G is maintained in memory by its weight matrix W = w ij


defined as:
wij = w(e) (if there is an edge from vi to vj)
= 0 (otherwise)

• Therefore, we define matrix Q by modifying Warshall’s algo. as:


Qk [i,j] = the smaller of the length of preceding path from vi to
vj or
= the sum of lengths of preceding paths from vi to vk
and from vk to vj

• Accordingly:
Qk [i, j] = Min (Qk-1 [i, j] , Qk-1 [i, k] + Qk-1 [k, j] )
Shortest Path (Modified Warshall’s algorithm)

A weighted graph G with M nodes is maintained in memory by


its weight matrix W. This algo. finds matrix Q such that Q[i,j] is
length of shortest path from node vi to vj.

1. Repeat for i, j = 1, 2, ........ M //[Initializes Q]


if W[i,j] = 0 then Set Q[i,j] = Infinity
else Set Q[i,j] = W[i,j]
2. Repeat Steps 3 and 4 for k = 1, 2, ...... M //[updates Q]
Repeat Step 4 for i = 1, 2, ...... M
Repeat for j = 1, 2, ...........M
Set Q[i,j] = Min (Q[i,j], Q[i,k] +
Q[k,j] )
3. Exit
Traversing a Graph
• Systematically examining the nodes and edges of a graph.

• Two methods

-Breadth First Search (BFS)


-Depth First Search (DFS)

• BFS will use Queue and DFS will use Stack as auxiliary storage

•The algorithm will process only those nodes which are reachable from the starting node

•During the execution of the algorithm, each node N of G will be in one of the three states, called
status of N

-STATUS = 1 : Ready State , i.e. initial state of node N

-STATUS = 2 : Waiting State , i.e. node N is in Queue or stack waiting to be processed

-STATUS = 3 : Processed State , i.e. node N has been processed


Breadth-First Search:

1.Initialize all nodes to ready state , STATUS = 1

2.Put the starting node A in Queue and change its status to waiting, STATUS = 2

3.Repeat step 4 and 5 until Queue is empty

4. Remove the front node N of Queue. Process N and change STATUS of N to


processed, STATUS = 3

5. Add to the rear of Queue all neighbors of N that are in ready state (STATUS
= 1) and change their STATUS to waiting state ( STATUS = 2 )

6.Exit
Node Adj
List
A F, C , B
B G, C
C F
D C
E D, C, J
F D
G C, E
J D, K
K E, G STEP QUEUE ORIGIN F R
1 A 0 1 1
2 AFCB 0AAA 2 4
3 AFCBD 0AAAF 3 5
4 AFCBD 0AAAF 4 5
5 AFCBDG 0AAAFB 5 6
6 AFCBDG 0AAAFB 6 6
7 AFCBDGE 0AAAFBG 7 7
8 AFCBDGEJ 0AAAFBGJ 8 8
9 … … .. ..
10 … …. .. ..
Applications of Breadth First Search

Shortest Path and Minimum Spanning Tree


for un-weighted graph
Peer to Peer Networks.
Crawlers in Search Engines
Social Networking Websites
GPS Navigation systems
Broadcasting in Network
In Garbage Collection
Depth-First Search:

1.Initialize all nodes to ready state , STATUS = 1

2.Push the starting node A in STACK and change its status to waiting,
STATUS = 2

3.Repeat step 4 and 5 until STACK is empty

4. Pop the TOP node N of STACK. Process N and change STATUS of N


to processed, STATUS = 3

5. Push onto STACK all neighbors of N that are in ready state (STATUS
= 1) and change their STATUS to waiting state ( STATUS = 2 )

6.Exit
Iterative

Recursive
Node Adj List
A F, C , B
B G, C
C F
D C
E D, C, J
F D
G C, E
J D, K
K E, G

STEP STACK POP

1 A A
2 FCB AB
3 FCG ABG
4 FCE ABGE
5 FCDJ ABGEJ
6 … …
Applications of Depth First Search

Finding minimum spanning tree


Detecting cycle in a graph
Path Finding
Topological Sorting
Solving puzzles with only one solution
Linked Representation of
Graph
D
A

B C

Nod Adjacency List


e
A B, C, D

B C

D C, E

E C
Sequential representation of graph G in
memory using adjacency matrix A has some
drawbacks.
It is difficult to insert/delete nodes in G (since size
of A needs to be changed & nodes to be
reordered).
Matrix A may be sparse (containing many zeros
for no edges) in which great deal of memory is
wasted.
Operation on Graph
Searching:
Find(info, link, start, item, loc)
Finds the location loc of first node containing item, or sets loc
= NULL

1. Set Ptr = start


2. Repeat while Ptr != NULL
If item = info[Ptr], then Set loc = ptr and return
else Set Ptr = link[Ptr]
3. Set loc = NULL and return
Deleting:
Delete(info, link, start, avail, item, flag)
Deletes first node in list containing item, or sets flag=False when
item doesn’t appear in list.

1. [List empty?] If start = NULL, then Set flag = False and return
2. [item in first node?] If info[start] = item, then
Set Ptr = start, start = link[start]
link[Ptr] = avail, avail = Ptr
flag = true and return
3. Set Ptr = link[start] and save = start [initializes pointers]
4. Repeat steps 5 and 6 while Ptr != NULL
5. If Info[Ptr] = item, then
Set link[save] = link[Ptr], link[Ptr] = avail
avail = Ptr, flag = True and return
Set save = Ptr and Ptr = link[Ptr] [updates pointers]
6. Set flag = False and return
Inserting:
InsNode(node, next, Adj, start, availN, N, flag)
This procedure inserts node N in graph G.

1. [Overflow?] If availN = NULL, then Set flag = False and


return
2. Set Adj[availN] = NULL [node is isolated]
3. [Removes node from availN list]
Set New = availN and availN = next[availN]
4. [Inserts node N in node list]
Set node[New] = N, next[New] = start, start = New
5. Set flag = True and return.

You might also like