Beruflich Dokumente
Kultur Dokumente
Graphs
Graph: A graph is a collection of nodes, called vertices, and a collection of segments called
lines, connecting pair of vertices. In other words, a graph consists of two sets, set of vertices and
a set of edges (lines).
1. Graphs may be either directed or undirected. A directed graph (or) digraph is a graph in
which each line has a direction (arrow head) to its successor. The lines in a directed graph
are known as arcs.
2. In a directed graph, the flow along the arcs between two vertices can follow only the
indicated direction.
3. An undirected graph is a graph in which there is no direction on any of lines, which are
known as edges. In an undirected graph, the flow between two vertices can go either
direction.
A A
B C D B C D
E F E F
i. Fig (a) Directed Graph (b) Undirected Graph
4. A path is a sequence of vertices in which each vertex is adjacent to the next one. In fig
(a): {A, B, C, F} is one path and {A, B, E, F} is another.
5. Two vertices in a graph are said to be adjacent vertices (or) neighbors if there is a path of
length 1 connecting them. In fig (a): B is adjacent to A, where as E is not adjacent to D.
In fig (b): E and D are adjacent, but D and F are not.
6. A cycle is a path consisting of at least three vertices that starts and ends with the same
vertex.
7. A loop is a special case of a cycle in which a single arc begins and ends with the same
vertex.
A B
C D
i. Fig (c)
8. Two vertices are said to be connected if there is a path between them. A graph is said to
be connected if, ignoring direction, there is a path from any vertex to any other vertex.
9. A directed graph is strongly connected if there is a path from each vertex to every other
vertex in the digraph.
10. A directed graph is weakly connected if at least two vertices are not connected.
B E F G
B E F G
Fig (e) strongly connected
C D H I
B E F G
Fig (f) Dis-joint graph
C D H I
The degree of a vertex is the number of lines incident to it. In fig (d), the degree of vertex B is 3
and the degree of vertex E is 4. The out degree of a vertex in a digraph is the number of arcs
leaving the vertex. The in degree of a vertex is the number of arcs entering the vertex. In fig (d)
for the vertex B the in degree is 1 and the out degree is 2.
Matrix representation is the most commonly useful way of representing any graph. This
representation uses a square matrix of order nXn, where n is the number of vertices in the graph.
The matrix is also known as adjacency matrix, because an entry shows the information whether
the two vertices are adjacent (or) not. It is also termed as bit matrix (or) boolean matrix as its
entries are either 1 (or) 0.
V1
V V B E
2 3
V V V V C D
4 5 6 7
The disadvantage of adjacency matrix representation is that it always requires a nXn matrix with
n vertices, regardless of the number of edges. The adjacency matrix for an undirected graph is
symmetric. The adjacency matrix for a directed graph need not be symmetric.
1234567 ABCD
1 0110000 A0 0 3 0
2 1001100 B5 0 1 7
3 1000011 C2 0 0 4
4 0100000 D0 6 8 0
6 0010000
7 0010000
Matrix Representation of G1
V1 V2 V3 \0
V2 V1 V4 V5 \0
V3 V1 V6 V7 \0
V4 V2 \0
V5 V2 \0
V6 V3 \0
V7 V3 \0
Fig (i): Representation of Graph G1.
A 3 C \0
C
D 26 A
B 84 D
C \0
UNIT VII GRAPHS
In the linked representation of graphs, the number of lists depends on the number of vertices in
the graph. The header node in each list maintains a list of all adjacent vertices of node.
Graph Algorithms:
Data structure is used for storing the graph. The most flexible structure is the adjacency list
implemented as a singly linked list.
GraphHead structure stores information about each vertex. Graph data is shown in GraphVertex
structure. GraphArc structure stores data about an arc i.e. path between two vertices.
Create Graph:
This algorithm initializes the metadata elements for graph data structure:
Algorithm: CreateGraph
1: set count to 0
2: set first to NULL
3: return GraphHead
End CreateGraph
Insert Vertex:
Insert vertex adds a disjoint, i.e. an unconnected, vertex to the graph. The arcs (path) associated
with the vertex must be inserted separately.
The algorithm is same as singly linked list insertion situation. After allocating memory for the
new vertex, we move in the data and set all of its necessary data values to NULL.
Delete Vertex:
In this algorithm, the first thing to do is we need to search for the vertex to be deleted is present
in the graph or not. Once we have found it, we need to ensure that there are no arcs leaving (or)
entering the vertex. If there is an arc, we reject the deletion.
This is basic singly linked list delete situation. The only complexity is that we cannot delete a
vertex if its degree is greater than 0.
1: return -2
2: end if
Locate vertex to be deleted.
3: search for vertex to be deleted
4: if (not found)
1: return -2
5: end if
Found vertex to be deleted
6: if (vertex inDegree > 0 or outDegree > 0)
1: return -1
7: end if
8: delete vertex
9: decrement GraphCount
10: return 1
End DeleteVertex
Insert Arc:
Once we have a vertex, we can connect it to other vertices. Insert Arc requires two points in the
graph: the source vertex and the destination vertex. Each vertex is identified by its key value
rather than by its physical address.
After allocating memory for the new arc, we locate the fromVertex and toVertex. This logic
involves simple linked list searches. If either search fails, we return the appropriate value, -2 for
fromVertex not found and -3 for toVertex not found. If we locate both vertices, we increase there
degree count and set the new arcs destination pointer to the destination vertex. Then, we can
insert the new arc into the adjacency list.
Delete Arc:
The delete arc algorithm removes one arc from the adjacency list. To identify an arc, we need
two vertices. The vertices are identified by their key. The algorithm therefore first searches the
vertex list for the start vertex and then searches its adjacency list for the destination vertex. After
locating and deleting the arc, the degree in the from and to vertices is adjusted.
The source vertex is located by searching the vertex list. Once we have located it, we search the
adjacency list for an arc that points to the destination vertex. Once we find the correct arc, we
adjust the degree fields in the from and to vertex entries and then delete the arc.
1: return -2
2: end if
Locate source vertex
3: search and set fromVertex to vertex with key equal to fromKey
4: if (fromVertex not found)
1: return -2
5: end if
Locate destination vertex in adjacency list
6: if (fromVertex arc list NULL)
1: return -3
7: end if
8: search and find arc with key equal to toKey
9: if (toKey not found)
1: return -3
10: end if
fromVertex, toVertex and arc all located then Delete arc
11: set toVertex to arcDestination
12: delete arc
13: decrement fromVertex outDegree
14: decrement toVertex inDegree
15: return 1
End DeleteArc
Retrieve Vertex:
Retrieve vertex return the data stored in a vertex. Given the key of the vertex, then the date are
placed in the output. This algorithm is a typical linked list search algorithm. If the search is
successful, the data in the vertex are placed in the output area specified in the calling sequence
and +1 is returned. If the list is NULL (or) if the data cannot be located, then return -2.
Spanning Tree:
A spanning tree has a property that for any pair of vertices there exists only one path between
them, and the insertion of any edge to a spanning tree form a unique cycle.
The spanning tree resulting from a call to depth first tree is known as depth first spanning tree.
The spanning tree resulting from a call to breadth first tree is known as a breadth first spanning
tree.
Algorithm:
Consider an example:
B D B
A F A A A F
C E C C
Fig (a) Fig (b) Fig (c)
B D
B D B D
A
A A F
C
C E C E
F
Fig (d) Fig (e) Fig (f)
(a) Insert first Vertex (b) Insert edge AC (c) Insert edge BC (d) Insert edge CD (e) Insert edge
DE (f) Insert edge DF
We can determine the shortest path of a graph by using the warshalls algorithm and prims
algorithm.
Warshalls Algorithm:
This algorithm determines if there is a path from any vertex V i to another vertex Vj either directly
(or) through one (or) more intermediate vertices. With this algorithm, we can test the reachability
of all pairs of vertices in a graph.
To compute the path matrix of a given graph, this algorithm is helpful. This algorithm treats the
entries in the adjacency matrix as bit entries and perform AND (^) and OR (v) boolean
operations on them.
Algorithm:
Input: A graph G whose pointer to its adjacency matrix is GPtr and vertices are labeled as 1, 2,
3, ., N; N being the number of vertices in the graph.
Output: The path matrix P
Data structure: Matrix representation of graph with pointer as GPtr.
Step 1: for i=1 to N do
1: for j=1 to N do
1: P [i][j] = GPtr [i][j]
2: end for
3: for k=1 to N do
1: for i=1 to N do
1: for j=1 to N do
1: P [i][j] = P [i][j] v (P [i][k] ^ P {k][j])
2: end for
2: end for
4: end for
5: return
6: stop
In step1, the path matrix P is initialized with the adjacency matrix GPtr, which signifies the
initial path matrix. This matrix, P [i][j], maintains path which are direct i.e. if there is a direct
path from Vi to Vj.
In step3, the outer most loop will execute N times. For each k, it decides whether there is a path
from Vi to Vj (for all i, j = 1, 2, ., N) either directly (or) via k, i.e., from V i to Vk and then from
Vk to Vj. It therefore sets the P [i][j] entries to 0 (or) 1 accordingly.
Consider an example:
1
12345
1 01101 2 3
2 00111
3 00010 4 5
4 00000
5 00110 K=1
During the execution of step3 in warshalls algorithm and when k=2 we see that
The final path matrix is obtained as follows. The final matrix shows the there is no path from the
vertex V4 to any other vertex.
12345
1 01111
2 00111
3 00010
4 00000
5 00110
Prims algorithm:
In the prims algorithm, minimum spanning tree grows in successive stages. At each stage in the
algorithm, we see that whether any of vertices have already been included in the tree, prims
algorithm then finds a new vertex to add it to the tree by choosing the edge <V i, Vj>, the smallest
among all edges, where Vi is the tree and Vj is yet to be included in the tree. The algorithm starts
by selecting a vertex arbitrarily, and then at each stage, we add an edge to the tree.
This algorithm can easily be implemented using adjacency matrix representation of a graph.
Suppose there is a nXn adjacency matrix for a given undirected weighted graph and the vertices
are labeled as V1, V2, V3, .., Vi, Vj, and Vn. Start from Vertex V1. Get its nearest neighbor, which is
the vertex that has the smallest entry in the row of V j. Let it be V1, now consider Vj and V1 as one
sub-graph, i.e., a vertex other than V1 and Vj that has the smallest entry among all the entries in
the rows of V1 and Vj, let his new vertex be V. Therefore, the tree now grows with vertex V 1, Vj,
and V as one sub-graph. This process is repeated until all N vertices have been connected by n-1
edges.
Consider the following example to find minimum spanning tree: An undirected weighted graph
and its weighted adjacency matrix is shown in following figure:
V1 1 2 3 4 5 6 7
1 - 4 5 2 - - -
V2 V3
2 4 - - 1 2 - -
3 5 - - 8 - 1 -
V4 4 2 1 8 - 3 4 7
5 - 2 - 3 - - 9
V5 V6
6 - - 1 4 - - 6
V7 7 - - - 7 9 6 -
Consider vertex V1 and pick the smallest entry in row 1 which is 2 in column 4; this V 4 is the
nearest neighbor to V1, the closest neighbor of sub-graph V 1 and V4 is V2 that can be seen by
examining all entries in rows 1 and 4. The remaining four edges relate to the above procedure.
V1 V1 V1
V2 V3 V2 V3 V2 V3
V4 V4 V4
V5 V6 V5 V6 V5 V6
DS NEC, CSE Dept. Page 13
V7 V7 V7
Final stage
An array selected [1..N] boolean is used and selected [i] = TRUE represents that i th vertex is
included in the tree. The minimum spanning tree will be stored in our adjacency matrix of order
NXN. In the adjacency matrix of given graph, we assume the [i, j] entry as infinity [a large +ive
value] if there is no edge between the vertices i and j.
Input: GPtr, in the form of adjacency matrix vertices are labeled as 1, 2, . N; N being the
number of vertices in the graph.
Output: TREE, the pointer to the minimum spanning tree represents with adjacency matrix of
order NXN.
1: for i = 1 to N do
Graph Traversals:
DFS:
DFS is similar to a preorder traversal in trees. The DFS begins by visiting the start vertex V. Next
we select an unvisited vertex, W, from Vs adjacency list and carry out a DFS on W. We preserve
our current position in Vs adjacency list by placing it on a stack. Eventually our search reaches a
vertex, U, which has no unvisited vertices on its adjacency list. At this point, we remove a vertex
from the stack and continue processing its adjacency list. Previously visited vertices are
discarded, unvisited vertices are placed on the stack. The search terminates when the stack is
empty. The DFS is best described recursively.
Consider an example in GraphVG2 which is represented by its adjacency lists. If a DFS is initiated
from vertex V1, then the vertices of G are visited in the order V1, V2, V4, V8, V5, V6, V3 and V7.
V1
V1 V2 V3 \0
V V V1 V4 V5 \0
2 3
V V V V V3 V1 V6 V7 \0
4 5 6 7
V4 V2 V8 \0
V8
(a) Graph
V5 V2 V8 \0
V6 V3 V8 \0
V7 V3 V8 \0
V8 V4 V5 V6 V7 \0
BFS:
BFS starts at vertex V and mark it as visited. It then visits each of the vertices on Vs adjacency
list. When we have visited all the vertices on Vs adjacency list, we visit all the unvisited vertices
that are adjacent to the first vertex on Vs adjacency list. To implement this scheme, as we visit
each vertex we place the vertex in a queue. When we have exhausted an adjacency list, we
remove a vertex from the queue and proceed by examining each of the vertices on its adjacency
list. Unvisited vertices are visited and then placed on the queue; visited vertices are ignored. The
search is finished when the queue is empty.
The BFS for the above graph G beginning at vertex V 1. Then we visit V1 and then V2 and V3.
Next vertices V4, V5, V6 and V7 will be visited and finally V8.
Applications:
Transitive Closure:
The Transitive closure matrix, denoted as A+, of a directed graph G, is a matrix such that
A+ [i][j] = 1; if there is a path of length > 0 from i to j;
A+ [i][j] = 0; Otherwise.
Graph
0 1 2 3 4
0 1 2 3 4
0. 0 1 0 0 0
1. 0 0 1 0 0
2. 0 0 0 1 0 Adjacency Matrix A
3. 0 0 0 0 1
4. 0 0 1 0 0
0 1 2 3 4
1. 0 1 1 1 1
2. 0 0 1 1 1
3. 0 0 1 1 1 Adjacency Matrix A+
4. 0 0 1 1 1
5. 0 0 1 1 1
Dijkstras shortest path:
Dijkstras shortest path is used to find the shortest path between two vertices in a network. For
example, if the network represents the routes flown by an airline, when we travel we want to find
the least expensive route between home and our destination. When the weights in the route graph
are the flight fare, our minimum cost is the shortest path between 2 nodes. Edsger dijkstra
developed a classic algorithm for just this problem. This algorithm is generally known simply as
Dijkstras shortest path algorithm.
DS NEC, CSE Dept. Page 17
UNIT VII GRAPHS
V V
2 2
1234
1. 75-- 11 12 - -
2. 7--2 21 - - 24
3. -3-- - 32 - -
4. 4-1- 41 - 43 -
Some of the values of adjacency matrix are infinity (-), which indicates that there is no direct
path between the vertices. The blank sign in right hand side matrix indicates that till now the path
is not determined.
The minimum cost between two vertices is determined by considering one vertex at one time.
Then the path from particular vertex is calculated. If the cost is found to be smaller then previous
cost then it is replaced with the new cost.
Consider the vertex 1 in the given example, the adjacency matrix now holds the values as
follows:
1 2 3 4
1. 7 5 - - 11 12 - -
2. 7 12 - 2 21 212 - 24
3. - 3 - - - 32 - -
4. 4 9 1 - 41 412 43 -
The values shown in square are the values that are added. Here the path 412 is possible as vertex
1 is considered. But the path 124 is not possible because till now the vertex 2 is not considered.
So, via 2 we cannot travel to any other vertex.
1 2 3 4
1. 7 5 - 7 11 12 - 124
2. 7 12 - 2 21 212 - 24
3. 10 3 - 5 321 32 - 324
4. 4 9 1 11 41 412 43 4124
Now the vertex 3 is considered, the paths between the vertices 4-2 and 4-4 are changed because
these are the shortest as compared to earlier ones.
1 2 3 4
1. 7 5 - 7 11 12 - 124
2. 7 12 - 2 21 212 - 24
3. 10 3 - 5 321 32 - 324
4. 4 4 1 6 41 432 43 4324
Finally, the vertex 4 is considered, which results into the adjacency matrix that holds the shortest
path between any two vertices.
1 2 3 4
1. 7 5 8 7 11 12 1243 124
2. 6 6 3 2 21 2432 243 24
3. 9 3 6 5 3241 32 3243 324
4. 4 4 1 6 41 432 43 4324