Anda di halaman 1dari 9

ASSIGNMENT 4

CSE- 2050

DATA STRUCTURE

SUBMITTED TO: SUBMITTED BY:


Mr. PARVEEN KUMAR RAVI RAJ
E6802B57
1. Following is the incidence matrix M of an undirected graph G:
0 0 1 0 0 1 1 1
0 1 0 1 0 0 1 0
1 0 1 1 0 0 0 0
0 0 0 0 1 0 0 1
1 1 0 0 1 1 0 0

Draw G and find its adjacency matrix A.

ANS.1
INCIDENCE MATRIX:- incidence matrix is defined as a matrix in which we assign value 1 if
node Vi belongs to edge Ej otherwise zero. Mathematically.

Mij= 1, if node Vi belongs to edge Ej.


0, if node Vi does not belongs to edge Ej.
This matrix is a i*j matrix where I gives number of nodes and j gives number of edges so in
above matrix we have 5 nodes and 8 edges.
Let nodes be V1, V2, V3, V4, V5.
Let edges be E1, E2, E3, E4, E5, E6, E7, E8.

So from above incidence matrix we can plot graph as follows.

Now from above graph we can find adjency matrix which is given by:

It is m*m matrix where m is number of nodes given by:

Aij= 1, if Vi is adjacent to Vj that is if edge exist between (Vi,Vj).


0, otherwise
So by above graph G.
Adjacent matrix is

1 1 1 1 1
1 1 1 0 1
1 1 1 0 1
1 0 0 1 1
1 1 1 1 1

2. Consider the weighted graph G, and assume the nodes are stored in an array
DATA as follows:
DATA:X, Y, S, T
7
X 3 Y
1
6 2
4
T S
5
(i) Find indegree and outdegree of each node.

ANS.2

1) In degree of each node

Node X- 2(from node Y and S)

Node Y-2(from node X and S)

Node S-1(from node T)

Node T-2(from node S and Y)

• Out degree of each node

Node X- 1(To node Y)


Node Y-2(To node X and T)
Node S-3(To node X,Y,T)
Node T-1(To node s)
(ii) Find the weight matrix W of G.
In given graph we define weight matrix as matrix

Wij= 1, if there is weight between node I to j.


0, otherwise
So we get:

0 7 0 0
W= 3 0 0 2
6 1 0 4
0 0 5 0

Q3.) Find the matrix Q of shortest paths using Warshall’s algorithm for the
graph given in Q2.
Ans.
Using warshall’s algorithm we get shortest path between node X and S as
follows:

7 2 5
X Y T S
Is the required shortest path which traverse all nodes of the given graph.
Part-B

Q4.) The Web can be modeled as a directed graph. Come up with a graph
traversal algorithm. Make the algorithm non-recursive and breadth-first.

Ans.4 Many real-world problems can be modeled using graphs. For example,
search engines model the Internet as a graph, where Web pages are the nodes in
the graph and the links among Web pages are the edges. Programs like
Microsoft MapPoint that can generate driving directions from one city to
another use graphs, modeling cities as nodes in a graph and the roads
connecting the cities as edges.

The edges of a graph provide the connections between one node and another.
By default, an edge is assumed to be bidirectional. That is, if there exists an
edge between nodes v and u, it is assumed that one can travel from v to u and
from u to v. Graphs with bidirectional edges are said to be undirected graphs,
because there is no implicit direction in their edges.

For some problems, though, an edge might infer a one-way connection from
one node to another. For example, when modeling the Internet as a graph, a
hyperlink from Web page v linking to Web page u would imply that the edge
between v to u would be unidirectional. That is, that one could navigate
from v to u, but not from u to v. Graphs that use unidirectional edges are said to
be directed graphs.

When drawing a graph, bidirectional edges are drawn as a straight line, as


shown in Figure 1. Unidirectional edges are drawn as an arrow, showing the
direction of the edge. Figure 2 shows a directed graph where the nodes are Web
pages for a particular Web site and a directed edge from u to v indicates that
there is a hyperlink from Web page u to Web page v. Notice that both u links
to v and v links to u, two arrows are used—one from v to u and another
from u tov.
Model of pages making up a website

5. Consider this graph:


v0 ----- v2
/ \
/ \
-> v1 <-/ \-> v4
/ \
/ \
/ \->v3 -------> v5
/ /
/ /
v6 <---------/
In what order are the vertices visited for a depth-first search that starts at v0? In
what order are the vertices visited for a breadth-first search that starts at v0?
Ans 5

 When we traverse above graph in depth first search with node v0 as root
then we get following order. We first traverse depth wise inspite of
complete level.

v0 ->v1 -> v3 ->v6->v5->v4->v2


 When we traverse above graph in breadth first search with node v0 as
root then we get following order. We first complete each level then go to
other level.

v0 -> v1 -> v4 -> v2 -> v3 -> v6 ->v5.

Q6.) Following is the adjacency matrix M of an undirected graph G:


01010
A= 10011
00011
11101
01110
Draw a graph G and find its incidence matrix M.

Ans, 6
1) Graph is
1

2 4

5 3

Incidence matrix-:

0 1 0 1 0
1 0 0 0 1
0 0 0 1 1
1 0 1 0 1
0 1 1 1 0
Q7.) Consider the following 4-digit employee number:
9614, 5882,6713,4409,1825
Find 2-digit hash address of each using
a) the division method with m=97;
b)midsquare method c)the folding method without reversing.

Ans.) a) The division method with m=97


H(K) = (K mod m)
Here m=97
i) H(9614)= (9614 mod 97)
= 11
Hence, a[11]= 2-digit hash address
ii) H(5882) = (5882 mod 97)
= 62
Hence, a[62]= 2-digit hash address
iii) H(6713) = (6713 mod 97)
= 20
Hence, a[20]= 2-digit hash address
iv) H(4409) = (4409 mod 97)
= 44
Hence, a[44]= 2-digit hash address

v) H(1825) = (1825 mod 97)


= 79
Hence, a[79]= 2-digit hash address

b) Mid-square method
i) 9614 = (61)^2
= 3721
Hence, 3721 is the location
ii) 5882 = (88)^2
= 7744
Hence, 7744 is the location
iii) 6713 = (71)^2
= 5041
Hence, 5041 is the location
iv) 4409 = (40)^2
= 1600
Hence, 1600 is the location
vi) 1825 = (82)^2
= 6724
Hence, 6724 is the location

c) The folding method without reversing


i) 9614 = 9+6+1+4= 20
ii) 5882 = 5+8+8+2 = 23
iii) 6713 = 6+7+1+3 = 17
iv) 4409 = 4+4+0+9 = 17
v) 1825 = 1+8+2+5 = 16

Anda mungkin juga menyukai