Transcript GRAPHS

Graphs
Graphs – Basic Concepts
 Basic definitions: vertices and edges
 More definitions: paths, simple paths, cycles,
loops
 Graph representation
 Adjacency matrix
 Adjacency lists
2
Graph
Definition: A graph is a collection (nonempty set)
of vertices and edges
Vertices: can have names and properties
Edges:
can be directed
Adjacent vertices: there is an edge between
them
3
Directed and undirected graphs
Graph2
A
Graph3
B
A
B
C
C
D
D
These are two different graphs
4
Undirected graph
3
0
0
2
1
degree
3 1
2 3
2
3
3
3
5
6
1
0
1 G2 1
in:1, out: 1
1
in: 1, out: 2
2
in: 1, out: 0
3
G13
4
1
Directed graph
in-degree
out-degree
G3
CHAPTER 6
5
More definitions : Cycle
Simple path with distinct edges, except
that the first vertex is equal to the last
ABCA
A
B
BACB
CBAC
C
D
A graph without cycles is called acyclic graph.
6
More definitions : Loop
An edge that connects the vertex with itself
A
C
B
D
7
A
Graphs and Trees
C
C
A
E
B
E
B
D
Tree1: root A
D
Tree2: root C
8
A spanning tree of an undirected
graph
A sub-graph that contains all the vertices, and no cycles.
If we add any edge to the spanning tree, it forms a cycle, and
the tree becomes a graph
A
B
A
B
graph
C
D
C
spanning tree
D
9
A
B
C
D
All spanning trees
of the graph on
the previous slide
Examples
A
B
C
D
A
B
C
D
10
Complete graphs
Graphs with all edges present – each vertex is
connected to all other vertices
A
B
C
D
E
A complete graph
Dense graphs:
relatively few of
the possible
edges are
missing
Sparse graphs:
relatively few of
the possible
edges are
present
11
Graph Representation
 Adjacency Matrix
 Adjacency Lists
 Linked Adjacency Lists
 Array Adjacency Lists
Adjacency Matrix
 n x n matrix, where n = No of vertices
 A[i,j] = 1 iff (i,j) is an edge
2
3
1
4
5
1
2
3
4
5
1
0
1
0
1
0
2
1
0
0
0
1
3
0
0
0
0
1
4
1
0
0
0
1
5
0
1
1
1
0
Adjacency Matrix Properties
2
3
1
4
5
1
2
3
4
5
1
0
1
0
1
0
2
1
0
0
0
1
3
0
0
0
0
1
4
1
0
0
0
1
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.
Adjacency Matrix (Digraph)
2
3
1
4
5
1
2
3
4
5
1
0
0
0
1
0
2
1
0
0
0
1
3
0
0
0
0
0
4
0
0
0
0
1
5
0
1
1
0
0
•Diagonal entries are zero.
•Adjacency matrix of a digraph need not be
symmetric.
Adjacency Matrix
 n2 bits of space
 O(n) time to find vertex degree and/or vertices
adjacent to a given vertex.
 O(1) time to determine if there is an edge between
two given vertices.
Adjacency Lists
 Adjacency list for vertex i is a linear list of vertices adjacent from
vertex i.
 An array of n adjacency lists.
aList[1] = (2,4)
2
3
1
aList[2] = (1,5)
aList[3] = (5)
aList[4] = (5,1)
4
aList[5] = (2,4,3)
5
Weighted Graphs
 Cost adjacency matrix.
 C(i,j) = cost of edge (i,j)
 Adjacency lists => each list element is a pair
(adjacent vertex, edge weight)
Linked Adjacency Lists
 Each adjacency list is a chain.
2
3
aList[1]
[2]
[3]
[4]
1
4
5
aList[5]
Array Length = n
# of chain nodes = 2e (undirected graph)
# of chain nodes = e (digraph)
2
4
1
5
5
5
1
2
4
3
0
4
0
1
2
1
2
6
3
3
0
1
2
3
1
0
0
0
7
2
2
1
1
G1
0
1
2
1
0
2
G3
5
3
3
3
2
0
1
2
0
1
2
3
4
5
6
7
1
0
0
1
5
4
5
6
G4
2
3
3
2
6
7
An undirected graph with n vertices and e edges ==> n head nodes and 2e list no
Graph Traversals
 DFT : Depth-First Traversal
 BFT : Breadth-First Traversal
Lecture 7: Graphs
21
Depth-First Traversal
 Use a stack (or recursion) to store set of vertices to be
visited
 From a given vertex, visit neighbors I+1 … N only after
traversing all possible edges from neighbor I
Lecture 7: Graphs
22
Depth-First Traversal
0
2
1
4
6
3
Lecture 7: Graphs
5
23
DFS (Non Recursive)
void dfs_nr(int i)
{
stack s, int j;
s.push(i);
while(!s.empty())
{
i=s.pop();
if(visited[i]!=1)
{
cout<<i;
visited[i]=1;
for(j=0; j<n ; j++)
{
if(visited[j]!=1 && G[i][j]==1)
{
s.push(j);
}
}//end of for
}//end of if
}//end of while
}//end of function
24
Recursive DFS
void DFT(int i)
{
int j;
cout<< i;
visited[i]=1;
for(j=0;j<n;j++)
if (!visited[j] && G[i][j]==1)
DFT(j);
}
25
Depth-First Search pseudo code
Algorithm DFS(i)
Input: A vertex i in a graph.
Output: Traversing of graph is recorded.
1. Initialize stack S
2. Push i in the stack
3. loop(stack not empt)
i <-pop element from stack
2.
if (! Visited[i])
1. set visited[i] to 1 and display it.
2. loop for each w adj. to I
1. if (!visited[w])
1. push w in stack
2. end if
3. end loop
3. end if
1.
End loop
End of function
4.
BFT non recursive
void BFT(int i)
{
Queue Q;
cout<< i;
Q.addq(i);
visited[i]=1;
while(!Q.empty())
{
i=Q.deleteq();
for(j=0; j<=n; j++)
{
if(visited[j]!=1 &&
G[i][j]==i)
{
visited[j]=1;
cout<< j;
Q.addq(j);
}//end if
}//end for loop
}//end while
27
Prim’s Algorithm
Aim: To find a minimum spanning tree T
 Step 1: Select any node to be the first node of T.
 Step 2: Consider the arcs which connect nodes in T
to nodes outside T. Pick the one with minimum weight.
Add this arc and the extra node to T. (If there are
two or more arcs of minimum weight, choose any one
of them.)
 Step 3: Repeat Step 2 until T contains every node of
the graph.
Complete Graph
4
B
4
A
C
2
4
E
1
F
2
D
1
3
10
5
G
5
6
3
4
I
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Old Graph
New Graph
4
B
4
A
C
2
4
E
1
1
F
A
5
G
6
C
2
4
E
3
10
5
4
1
2
D
4
B
F
2
D
1
3
10
5
G
3
5
4
6
3
I
4
H
I
2
J
3
H
2
J
3
Complete Graph
4
B
4
A
Minimum Spanning Tree
C
2
4
E
1
1
F
A
5
G
6
C
2
E
3
10
5
4
1
2
D
4
B
1
F
2
D
G
3
3
4
I
H
I
2
J
3
H
2
J
3
Prim’s Algorithm
int GraphMatrix :: prim(GraphMatrix &spanning)
{
int cost[MAX][MAX], dist[MAX], source[MAX], visited[MAX];
int u, v, mindist, edges, i, min_cost, j;
for(i = 0; i < num; i ++)// creation of cost matrix
{
for(j = 0; j < num; j ++)
{
if(array[i][j] == 0)
cost[i][j] = INFINITY;
else
cost[i][j] = array[i][j];
spanning.array[i][j] = 0;
}
}
41
dist[0] = 0;
visited[0] = 1;
for(i = 1; i < num; i ++)
{
dist[i] = cost[0][i];
source[i] = 0;
visited[i] = 0;
}
min_cost = 0;
edges = num - 1;
42
while(edges > 0)
{
mindist = INFINITY;
for(i = 1; i < num; i ++)
if(visited[i] == 0 && dist[i] < mindist)
{
v = i;
mindist = dist[i];
}
u = source[v];
spanning.array[u][v] = dist[v];
spanning.array[v][u] = dist[v];
edges --;
visited[v] = 1;
for(i = 1; i < num; i ++)
if(visited[i] == 0 && cost[i][v] < dist[i])
{
dist[i] = cost[i][v];
source[i] = v;
}
min_cost = min_cost + cost[u][v];
}
spanning.num = num;
return(min_cost);
}
43
Analysis of Prim's Algorithm
Running Time = O(n*n)
Kruskal’s Algorithm
Aim: To find a minimum spanning tree for a connected
graph with n nodes:
 Step 1: Choose the arc of least weight.
 Step 2: Choose from those arcs remaining the arc of
least weight which does not form a cycle with already
chosen arcs. (If there are several such arcs, choose
one arbitrarily.)
 Step 3: Repeat Step 2 until n – 1 arcs have been
chosen.
Complete Graph
4
B
4
A
C
2
4
E
1
F
2
D
1
3
10
5
G
5
6
3
4
I
H
2
J
3
4
B
4
A
C
2
4
E
1
B
A
1
D
B
4
C
B
4
D
B
10
J
C
2
E
C
1
F
D
5
H
D
6
J
E
2
G
F
3
G
F
5
I
G
3
I
G
4
J
H
2
J
I
3
J
1
3
10
5
G
5
4
F
2
D
A
6
3
4
I
H
2
J
3
Sort Edges
(in reality they are placed in a priority
queue - not sorted - but sorting them
makes the algorithm easier to
visualize)
4
B
4
A
C
2
4
E
1
D
C
1
F
C
2
E
E
2
G
H
2
J
F
3
G
G
3
I
I
3
J
A
4
B
B
4
D
B
4
C
G
4
J
F
5
I
D
5
H
D
6
J
B
10
J
1
3
10
5
G
5
1
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}
5
{1,4}
{1, 2, 3,4, 5} {6,7}
6
{2,5}
rejected
7
{4,7}
{1, 2, 3,4, 5,6,7}
62
Vertex no  1
2
1
3
2
4
3
5
4
6
5
7
6
7
Component no
After adding {1,2}
Vertex no  1
1
2
1
3
3
4
4
5
5
6
6
7
7
Component no
63
After adding {2,3}
Vertex no  1
2
1
3
1
4
1
5
4
6
5
7
6
7
Component no
After adding {4,5}
Vertex no  1
1
2
1
3
1
4
4
Component no
5
4
6
6
7
7
Kruskal Algorithm
Kruskal (G =<N,A>: graph; length: A R+ ): set of edges
{initialization}
Sort A by increasing length
n the no of nodes
T  {will contain the edges of the minimum Spanning Tree }
Loop(T contains n-1 edges)
e  {u,v}  shortest edge not yet consider
ucomp  find(u)
vcomp  find(v)
if (ucomp !=vcomp) then
merge (ucomp,vcomp)
T  T U {e}
End Loop
return T
65
Analysis of Kruskal's Algorithm
Sorting edges
 O(e lg e)
Spanning tree iteration , this involves merging of two components
O(lg n) and merging edges. O(e lg n)
Overall  O(e lg e) + O(e lg n)
Shortest path
Dijkstra(L[1..n,1..n])
Array D[2..n]
{ initialization}
c{2,3..n}
Loop i 2 to n do D[i]L[1,i]
Repeat n-2 times
V some element in C which
is minimized D[v]
C C\{v} //remove it from C
For each w belongs to C do
D[w]
min(D[w],D[v]+L[v,w])
Return D
End .
67