Beruflich Dokumente
Kultur Dokumente
Chapter IV:
Data Structures
FAP:UC-BCF
Contents
Graphs
- Terminology
- Graph Representation
- Graph Traversal
- Spanning Trees
- Kruskals Algorithm
FAP:UC-BCF
Graphs
A graph G, consists of two sets V and E.
V is a finite non-empty set of vertices. V(G) = {Vi, VjVn}
E is the set of pairs of vertices. E(G) = {(Vi,Vj),(Vk,Vl)}
These pair of vertices are called edges.
Example of Undirected Graphs G1 and G2:
1
4
FAP:UC-BCF
Kinds of Graphs
Undirected Graph
Directed Graph
FAP:UC-BCF
Kinds of Graphs
Undirected Graph
Directed Graph
FAP:UC-BCF
Graphs
Adjacent and Incidents
1
FAP:UC-BCF
Graphs
Adjacent and Incidents
1
FAP:UC-BCF
Graphs
Adjacent and Incidents
FAP:UC-BCF
Subgraphs
A subgraph of G is a graph G such that V(G)<=V(G)
And E(G)<=E(G).
G1
Some Subgraphs of G1
FAP:UC-BCF
Graphs
Path
Simple Path
A path wherein all vertices except the first and the last are
distinct
Cycle
A simple path where the first and the last vertices are the
same.
FAP:UC-BCF
10
Graphs
Example:
1
1
1
4
2
G1
G2
3
5
G3
Graphs
In-degree
The number of edges for which the vertex is the head/2nd component.
Out-degree
The number of edges for which the vertex is the tail/1st component.
FAP:UC-BCF
12
Graphs
Example:
1
1
4
G4
Degree of vertex 2 = 3
Degree of vertex 3 = 2
FAP:UC-BCF
3
5
G3
Degree of vertex 1 = 3
where in-degree of vertex 1 = 1
plus out-degree of vertex 1 = 2
13
Graphs
Connected
An undirected/directed graph is said to be connected if for
every pair of distinct vertices Vi, Vj in V(G), there is a path from
Vi to Vj.
FAP:UC-BCF
14
Graph Representation
Adjacency Matrix
Adjacency matrix of an N vertex graph G is a two-dimensional array
(NxN), say A, with the property such that A[i,j] = 1 IFF the edge
(Vi, Vj) ((<Vi, Vj>) for directed graph) is in E(G). Otherwise, A[i,j]=0
1
G3
FAP:UC-BCF
Note:
Row sum of vertex1 = out-degree of vertex1
Column sum of vertex1 = in-degree of vertex1
15
Graphs
1
G4
1
2
3
4
5
6
0
1
1
0
0
0
1
0
0
1
1
0
1
0
0
0
0
1
0
1
0
0
0
0
0
1
0
0
0
0
0
0
1
0
0
0
Note:
Row sum of vertex1 = degree of vertex1
FAP:UC-BCF
16
Graph Representation
Adjacency List
N rows of the adjacency matrix are represented as N linked lists.
The nodes in each list represented the vertices adjacent from vertexl.
Each node has at least 2 fields: vertex and link.
1
G3
FAP:UC-BCF
17
Graph Traversals
FAP:UC-BCF
18
Graph Traversals
FAP:UC-BCF
19
Graph Traversals
Example:
1
DFS @1: 1, 2, 4, 8, 5, 6, 3, 7
BFS @1: 1, 2, 3, 4, 5, 6, 7, 8
FAP:UC-BCF
20
Graph Traversals
7
Exercise:
8
4
12
1
10
13
3
5
11
14
6
BFS @7:
BFS @8:
FAP:UC-BCF
21
Spanning Trees
FAP:UC-BCF
22
Spanning Trees
1
DF Spanning Tree
FAP:UC-BCF
BF Spanning Tree
23
FAP:UC-BCF
24
Kruskals ALgorithm
The minimum cost spanning tree is built, edge by edge. An edge
is included in the tree if it does not form a cycle with edges
already in the tree.
Algorithm:
T:={}
While T contains less than n-1 edges and E is not empty do
Begin
Choose an edge(v,w) from E of lowest cost;
Delete (v,w) from E.
If(v,w) does not create a cycle in T then
Add (v,w) to T
else
Discard (v,w)
End;
If T contains fewer than n-1 edges then writeln(No spanning tree)
Note: E is a set of Edges in G. T will contain edges for the final minimum cost spanning tree.
FAP:UC-BCF
25