Sie sind auf Seite 1von 6

HOMEWORK NO:4

of
Data Structure
(CSE2050)

SUBMITTED TO-:
SUBMITTED BY-:
Mam karnvirn
Philip sunny
Rd6803b34
10804895

Part-A

Q1. ) 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.) Graph is

0 4

2 5 6 7 1

Adjacency matrix-:

10101111
01011010
10110000
01110001
11001100
10001100
11000010
10010001
Q2.) 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
S T
5
(i) Find indegree and outdegree of each node
Ans. ) indegree(X)-: 2
outdegree (X)-: 1
indegree(Y)-: 2
outdegree (Y)-:2
indegree(S)-: 1
outdegree (S)-: 3
indegree (T)-: 2

outdegree (T)-: 1

(ii) Find the weight matrix W of G.


Ans. )

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 Q3.

Ans.) Shortest paths using Warshall’s algorithm is

7 2 5
X Y T S
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.) 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.
Figure 2. Model of pages making up a website

Q5.) 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.) For depth first search:- v0,v1,v3,v6,v5,v4,v2


For breadth first search:- 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.) Graph is
0

1 3

4
2

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)
= 9914 %97=11
Hence, a[11]= 2-digit hash address

ii) H(5882) = (5882 mod 97)


= 5882%97 = 62
Hence, a[62]= 2-digit hash address
iii) H(6713) = (6713 mod 97)=6713%97
= 20
Hence, a[20]= 2-digit hash address
iv) H(4409) = (4409 mod 97)=4409%97
= 44
Hence, a[44]= 2-digit hash address
v) H(1825) = (1825 mod 97) =1825%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

Das könnte Ihnen auch gefallen