14
MODULE 7 Graph & their Applicatioins 7.1 I n tr o d u c t io n to G r aph G r ap h i san i m p ortant n on l i n ea r data s t r uct u r e. T his d ata s t r u c t u re is u s ed t o r ep r es en tr el at i o n s h i p b et w een p ai rs o f elem en ts, w h i ch m ay n o t, n ecessari l y, b e h ier arch i cal i n n ature.

Chapter 7 Graph Their Applications

Embed Size (px)

Citation preview

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 1/14

MODULE

7Graph

& their

Applicatioins

7.1 Introduction to Graph

Graph is an important non linear data structure. This data

structure is used to represent relationship between pairs of

elements, which may not, necessarily, be hierarchical in nature.

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 2/14

A graph is defined as: “Graph G is an ordered set (V, E), where

V(G) represents the set of elements, called vertices, and E(G)

represents the edges between these vertices.”

Graphs can be of either type,

0 Undirected Graph

1 Directed Graph

Figures below show sample graphs.

V(G) = { v1, v2, v3, v4, v5 }

E(G) = { e1, e2, e3, e4, e5 }

Undirected Graph – In the undirected graph the pair of vertices

representing any edge is unordered. Thus, the pairs (v1,v2) and

(v2,v1) represent the same edge.

Directed Graph - In a directed graph each edge is represented by

a directed pair <v1,v2>. V1 is the tail and v2 the head of the edge.

Therefore <v2,v1> and <v1,v2> represent two different edges.

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 3/14

7.2 Graph Theory Terminologies

Adjacent Vertices

An edgee is represented by a pair of vertices denoted by[u, v].

The verticesu andv are called endpoints of edgee. These vertices

are also called adjacent vertices or neighbors.

Degree of a Vertex

The degree of vertexu, written asdegree(u)or d(u), is thenumber of edges containingu. Ifd(u) = 0, this means that vertexu

does not belong to any edge, then vertexu is called an isolated

vertex.

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 4/14

Path

A pathP of lengthn from a vertexu to vertexv is defined as

sequence of(n + 1) vertices, i.e., P = (v 1, v 2, v 3 …v n+1). The

path is said to be closed if the endpoints or end-vertices of thepath are same, i.e., v 1 = v n+1. The path is said to be simple if all

the vertices in the sequence are distinct, with the exception that v

1 = v n+1. In latter case, it is known as closed simple path.

Cycle

A cycle is closed simple path with length of 2 or more.

Sometimes, a cycle of lengthk (i.e.,k distinct vertices in the path)is known as k-cycle.

Connected Graph

A graph is said to be connected if there exist a path between anytwo of its vertices, i.e., an isolated vertex does not exist. A

connected graph without any cycles is called a tree. Thus we can

say that tree is a special graph.

Complete Graph

A graphG is said to be complete or fully connected if there exist a

path from every vertex to every other vertex. A complete graphwithn vertices will have(n (n-1))/2edges.

Weighted Graph

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 5/14

A graph is said to be a weighted graph if every edge in the graph

is assigned some data or value. The weight is denoted byw(e).

w(e) is non negative value that may be representing the cost of

moving along that edge or distance between the vertices.

Multiple Edges

 Distinct edgese ande’ are called multiple edges if they connect

the same end points, i.e., if e = [u, v] and e’ = [u, v].

Loop

An edge is a loop if it has identical endpoints, i.e., if e = [u, u].

7.3 Sequential representation of Graph

The most commonly used representations for graphs are

 Adjacency matrices and Adjacency Lists.

 

7.3.1 Adjacency Matrices

The adjacency matrix is a two dimensional array of size nxn

(where n is the number of vertices in the graph) with the property

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 6/14

that a[i][j]=1 if edge (vi,vj) is in the set of edges and a[i[[j]=0, if

there is no such edge.

Logical representation of graph.

 

The adjacency matrix for our graph is given below.A B C D E F

A

B

C

D

E

F

-- 1 1 1 -- --1 -- 1 -- 1 --

1 1 -- -- -- --

1 -- -- -- 1 1

-- 1 -- 1 -- --

-- -- -- 1 -- --

Notice that the matrix shown above has six rows and six columnslabeled with the nodes from the graph. We mark a '1' in a cell if

there exists an edge from two nodes that index that cell. For

example, since we have a edge between A and B, we mark a '1' in

the cells indexed by A and B. These cells are marked with a dark

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 7/14

gray background in the adjacency matrix. With our adjacency

matrix, we can represent every possible edge that our graph can

have.

7.3.2 Adjacency List

 

In the representation the n rows of the adjacency matrix are

represented as n linked lists. There is one list for each vertex in the

graph. The node in list I represent the vertices that are adjacent

from vertex i. each list has a head node. The head nodes are

sequential providing easy random access to the adjacency lists.

7.4 Spanning Tree

A spanning tree of a graph is an undirected tree consisting of onlythose edges that are necessary to connect all the vertices in the

original graph. A spanning tree has a property that for any pair of

vertices there exist only one path between them, and the insertion

of any edge to a spanning tree form a unique cycle.The particular

spanning tree for a graph depends on the criteria used for

generating it.

The spanning tree is useful in-1) analysis of electrical circuits

2) shortcut route problems

7.5 Constructing a Spanning Tree

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 8/14

Prim Algorithm

Given a connected weighted graph G, it is often desired to create

a spanning tree T for G such that the sum of the weights of the

tree edges in T is as small as possible. Such a tree is called as

minimum spanning tree and represents the most inexpensive way

of connecting all the nodes in G.

There are a number of techniques for creating a minimum

spanning tree for a weighted graph. The first of these, Prim’s

algorithm, discovered independently by Prim and Dijkstra, is very

much like Dijkstra’s algorithm for finding shortest paths. An ar-

 bitrary node is chosen initially as the tree root (note that in an

undirected graph and its spanning tree, any node can be

considered the tree root and the nodes adjacent to it as its sons).

The nodes of the graph are then appended to the tree one at a

time until all nodes of the graph are included. The node of the

graph added to the tree at each point is that node adjacent to anode of the tree by an arc of minimum weight. The arc of

minimum weight becomes a tree arc connecting the new node to

the tree. When all the nodes of the graph have been added to the

tree, a minimum spanning tree has been constructed for the

graph.

To see that this technique creates a minimum spanning tree,consider a minimum spanning tree T for the graph and consider

the partial tree PT built by Prim’s algorithm.

Suppose that (a, b) is the minimum-cost arc from nodes in PT to

nodes not in PT, and suppose that (a, b) is not in T. Then, since

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 9/14

there is a path between any two graph nodes in a spanning tree,

there must be an alternate path between a and b in T that does not

include arc (a, b). This alternate path P must include an arc (x, y)

from a node in PT to a node outside of PT. Let us assume that Pcontains sub-paths between a and x and between y and b.

Now consider what would happen if we replaced arc (x, y) in T

with (a, b) to create NT. We claim that NT is also a spanning tree.

To prove this, we need to show two things: that any two nodes of

the graph are connected in NT and that NT does not contain a

cycle, that is, that there is only one path between any two nodes inNT.

Since T is a spanning tree, any two nodes, m and n, are connected

in T. If the path between them in T does not contain (x, y), the

same path connects them in NT. If the path between them in T

does contain (x, y), consider the path in NT formed by the sub-

path in T from m to x, the sub-path in P (which is in T) from x to

a, the arc (a, b), the sub-path in P from b to y, and the sub-path in

T from y to n. This is a path from m to n in NT. Thus any two

nodes of the graph are connected in NT.

To show that NT does not contain a cycle, suppose that it did. If

the cycle does not contain (a, b) then the same cycle would exist in

T. But that is impossible, since T is a spanning tree. Thus the cycle

must contain (a, b). Now consider the same cycle with arc (a, b)replaced with the sub-path of P between a and x, the arc (x, y),

and the sub-path in P between y and b. The resulting path must

also be a cycle and is a path entirely in T. But, again, T cannot

contain a cycle. Therefore NT also does not contain a cycle.

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 10/14

NT has thus been shown to be a spanning tree. But NT must have

lower cost than T, since (a, b) was chosen to have lower cost than

(x, y). Thus it is not a minimum spanning tree unless it includes

the lowest weight arc from PT to nodes outside PT. Therefore anyarc added by Prim’s algorithm must be part of a minimum

spanning tree.

The crux of the algorithm is a method for efficient determination

of the “closest” node to a partial spanning tree. Initially, when the

partial tree consists of a single root node, the distance of any other

node n from the tree, distance[n], is equal to weight(root ,n).When a new node, current, is added to the tree, distance[n] is

modified to the minimum of distance[n] and weight(current, n).

The node added to the tree at each point is the node whose

distance is lowest. For nodes m in the tree, distance[m] is set to

infinity, so that a node outside the tree is chosen as closest. An

additional array closest[n] points to the node in the tree such that

distance[n] = weight(closest[n], n); that is, the node in the treeclosest to n. If two nodes, x and y, are not adjacent, weight(x, y) is

also infinity.

Kruskal Algorithm

An algorithm which is used to create a minimum spanning tree is

attributed to Kruskal's algorithm. In this algorithm a minimum

cost Spanning tree T is built edge by edge. Edges are considered

for inclusion in T in increasing order of their costs. An edge is

included in T if it does not form a cycle with edges already in T.

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 11/14

The nodes of the graph are initially considered as n distinct

partial trees with one node each. At each step of the algorithm,

two distinct partial trees are connected into a single partial tree by

an edge of the graph. When only one partial tree exists (after n-1such steps), it is a minimum spanning tree.

The concern is what connecting arc to use at each step. The

answer is to use the arc of minimum cost that connects two

distinct trees. To do this, the arcs can be placed in a priority queue

 based on the weight. The arc of lowest weight is then examined to

see if it connects two distinct trees.

To determine if an arc (x, y) connects distinct trees, we can

implement the trees with a father field in each node. Then we can

traverse all ancestors of x and y to obtain the roots of the trees

containing them. If the roots of the two trees are the same node, x

and y nodes are already in the same tree, arc (x, y) is discarded,

the arc of next lowest weight is examined. Combining two treessimply involves setting the father of the root of one to the root of

the other.

Algorithm

a) Forming the initial priority queue, i.e., O(e log e)

 b) Removing the minimum weight arc and adjusting the priority

queue, i.e., O(log e).c) Locating the root of a tree, i.e., O(log n).

d) Initial formation of n trees, i.e., O(n).

e) Assume n < e.

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 12/14

Every tree has a root node, and all the other nodes in the tree are

children of a perticular node. The nodes can have many children

 but only one parent. When we relax these restrictions, we get the

graph data structure. The logical representation of a typical graphmight look something like the figure shown below:

It is not hard to imagine how the graph data structure could be

useful for representing the data. Perhaps each of the nodes above

could represent a city and the edges connecting the nodes could

represent the roads. Or we could use a graph to represent a

computer network where the nodes are workstations and the

edges are the network connections. Graphs have so many

applications in the computer science and mathematics that severalalgorithms have been written to perform the standard graph

operations such as searching the graph and finding the shortest

path between nodes of a graph.

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 13/14

Notice that our graph does not have any root node like the tree

data structure. Instead, any node can be connected with any other

node in the graph. Nodes do not have any clear parent-child

relationship like we saw in the tree. Instead nodes are called asneighbors if they are connected by an edge. For example, node A

above has three neighbors: B, C, and D.

A graphconsists of a set ofnodes(orVertices) and a set ofarc(or

edge). Each arc in a graph is specified by a pair of nodes. A node n

isincidentto an arc x if n is one of the two nodes in the ordered

pair of nodes that constitute x. Thedegreeof a node is the number

of arcs incident to it. Theindegreeof a node n is the number of arcs

that have n as the head, and the outdegree of n is the number of

arcs that have n as the tail

The graph is the nonlinear data structure. The graph shown in the

figure represents 7 vertices and 12 edges. The Vertices are { 1, 2, 3,

4, 5, 6, 7} and the arcs are {(1,2), (1,3), (1,4), (2,4), (2,5), (3,4), (3,6),

(4,5), (4,6), (4,7), (5,7), (6,7) }. Node (4) in figure has indegree 3,

outdegree 3 and degree 6.

8/12/2019 Chapter 7 Graph Their Applications

http://slidepdf.com/reader/full/chapter-7-graph-their-applications 14/14