gnanavelmeblog.files.wordpress.com  · web viewbreadth first search (bfs) of a graph g starts from...

10
RAJALAKSHMI ENGINEERING COLLEGE [AUTONOMOUS] CAT3-ANSWER KEY SUJECT CODE / NAME: CS17201- DATA STRUCTURES SEMESTER: III YEAR: II-ECE DATE: 13-10-2018 Answer ALL Questions Part A [4 x 2 = 8 Marks] 4.1 Define Graph. Give example. A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E. Vertices are referred to as nodes. Each edge is a pair (v, w), where v, w V i.e. v = V1, w = V2. Edges are sometimes referred to as arcs. Here V1, V2, V3 and V4 are vertices and (V1, V2), (V1, V3), (V1, V4), (V2, V3), (V2, V4) and (V3, V4) are edges. 4.2 Define directed and Undirected Graph. Directed Graph: Directed graph is a graph which consists of directed edges, where each edge in E is unidirectional. It is also referred as Digraph. If (v, w) is a directed edge then (v, w) # (w, v). Undirected Graph: An undirected graph is a graph, which consists of undirected edges. If (v, w) is an undirected edge then (v, w) = (w, v). 4.3 Find the Depth First Traversal for the given graph. Possible Answers: 0, 1, 4, 5, 6, 2, 3, 7, 8 0, 1, 7, 8, 4, 5, 6, 2, 3 0, 2, 3, 4, 5, 6, 1, 7, 8 0, 1, 4, 5, 6, 2, 3, 8, 7 0, 1, 7, 8, 4, 6, 5, 2, 3 0, 2, 3, 4, 6, 5, 1, 8, 7 0, 1, 4, 6, 5, 2, 3, 7, 8 0, 1, 8, 7, 4, 5, 6, 2, 3 0, 2, 3, 4, 5, 6, 1, 8, 7 0, 1, 4, 6, 5, 2, 3, 8, 7 0, 1, 8, 7, 4, 6, 5, 2, 3 0, 2, 3, 4, 6, 5, 1, 7, 8

Upload: vuongdieu

Post on 20-Jan-2019

221 views

Category:

Documents


0 download

TRANSCRIPT

RAJALAKSHMI ENGINEERING COLLEGE [AUTONOMOUS]

CAT3-ANSWER KEY

SUJECT CODE / NAME: CS17201- DATA STRUCTURES

SEMESTER: III YEAR: II-ECE DATE: 13-10-2018

Answer ALL QuestionsPart A [4 x 2 = 8 Marks]

4.1 Define Graph. Give example.A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E. Vertices are referredto as nodes. Each edge is a pair (v, w), where v, w V i.e. v = V1, w = V2. Edges are sometimes referred to as arcs.

Here V1, V2, V3 and V4 are vertices and (V1, V2), (V1, V3), (V1, V4), (V2, V3), (V2, V4) and (V3, V4) are edges.

4.2 Define directed and Undirected Graph.Directed Graph: Directed graph is a graph which consists of directed edges, where each edge in E is unidirectional. It is also referred as Digraph. If (v, w) is a directed edge then (v, w) # (w, v).Undirected Graph: An undirected graph is a graph, which consists of undirected edges. If (v, w) is an undirected edge then (v, w) = (w, v).

4.3 Find the Depth First Traversal for the given graph.

Possible Answers: 0, 1, 4, 5, 6, 2, 3, 7, 8 0, 1, 7, 8, 4, 5, 6, 2, 3 0, 2, 3, 4, 5, 6, 1, 7, 80, 1, 4, 5, 6, 2, 3, 8, 7 0, 1, 7, 8, 4, 6, 5, 2, 3 0, 2, 3, 4, 6, 5, 1, 8, 70, 1, 4, 6, 5, 2, 3, 7, 8 0, 1, 8, 7, 4, 5, 6, 2, 3 0, 2, 3, 4, 5, 6, 1, 8, 70, 1, 4, 6, 5, 2, 3, 8, 7 0, 1, 8, 7, 4, 6, 5, 2, 3 0, 2, 3, 4, 6, 5, 1, 7, 80, 2, 4, 6, 5, 1, 7, 8, 3 0, 2, 4, 6, 5, 1, 7, 8, 3 0, 2, 4, 5, 6, 1, 8, 7, 30, 2, 4, 6, 5, 1, 8, 7, 3

4.4 Represent the Graph (Ref Qn.4.3) using Adjacent Matrix and Adjacent ListAdjacency Matrix:

0 1 2 3 4 5 6 7 80 0 1 1 0 0 0 0 0 01 1 0 0 0 1 0 0 1 12 1 0 0 1 1 0 0 0 03 0 0 1 0 0 0 0 0 04 0 1 1 0 0 1 1 0 05 0 0 0 0 1 0 0 0 06 0 0 0 0 1 0 0 0 07 0 1 0 0 0 0 0 0 18 0 1 0 0 0 0 0 1 0

Adjacency List:

Part B [1x7=7 Marks]Question No.4.5 is Compulsory

4.5 i. Explain in detail about Breadth First Traversal. (4)(Any one explanation)Breadth First Search (BFS) of a graph G starts from an unvisited vertex u. Then all unvisitedvertices vi adjacent to u are visited and then all unvisited vertices wj adjacent to vi are visited and so on. The traversal terminates when there are no more nodes to visit. Breadth first search uses a queue data structure to keep track of the order of nodes whose adjacent nodes are to be visited.Routine for BFS:def BFS(self, s):

visited = [False] * (len(self.graph))queue = []queue.append(s)visited[s] = Truewhile queue:

s = queue.pop(0)print (s, end = " ")for i in self.graph[s]:

if visited[i] == False:queue.append(i)visited[i] = True

Algorithm:Step 1: Choose any node in the graph, designate it as the search node and mark it asvisited.Step 2: Using the adjacency matrix of the graph, find all the unvisited adjacent nodes to thesearch node and enqueue them into the queue Q.Step 3: Then the node is dequeued from the queue. Mark that node as visited anddesignate it as the new search node.Step 4: Repeat step 2 and 3 using the new search node.Step 5: This process continues until the queue Q which keeps track of the adjacent nodes isempty.

ii. Find the Breadth First Traversal for Graph given in Qn 4.3. (3)Possible Answers:0, 1, 2, 7, 8, 4, 3, 5, 6 0, 1, 2, 7, 8, 4, 3, 6, 5 0, 2, 1, 3, 4, 7, 8, 5, 6 0, 2, 1, 3, 4, 7, 8, 6, 50, 1, 2, 7, 4, 8, 3, 5, 6 0, 1, 2, 7, 4, 8, 3, 6, 5 0, 2, 1, 4, 3, 7, 8, 5, 6 0, 2, 1, 4, 3, 7, 8, 6, 50, 1, 2, 8, 7, 4, 3, 5, 6 0, 1, 2, 8, 7, 4, 3, 6, 5 0, 2, 1, 3, 4, 8, 7, 5, 6 0, 2, 1, 3, 4, 8, 7, 6, 50, 1, 2, 8, 4, 7, 3, 5, 6 0, 1, 2, 8, 4, 7, 3, 6, 5 0, 2, 1, 4, 3, 8, 7, 5, 6 0, 2, 1, 4, 3, 8, 7, 6, 50, 1, 2, 4, 7, 8, 3, 5, 6 0, 1, 2, 4, 7, 8, 3, 6, 50, 1, 2, 4, 8, 7, 3, 5, 6 0, 1, 2, 4, 8, 7, 3, 6, 5

Part C [1x10=10 Marks]

4.6 a. i. Define Topological Ordering. (2)A topological sort is an ordering of vertices in a directed acyclic graph, such that if there is apath from Vi to Vj, then Vj appears after Vi in the ordering.

ii. Write algorithm to find the Topological Ordering of the vertices (4)Step 1: Find the indegree for every vertex.Step 2: Place the vertices whose indegree is 0 on the empty queue.Step 3: Dequeue the vertex v and decrement the indegree of all its adjacent vertices.Step 4: Enqueue the vertex on the queue if its indegree falls to zero.Step 5: Repeat from step 3 until the queue becomes empty.Step 6: The topological ordering is the order in which the vertices dequeue.

(or)def topologicalSortUtil(self,v,visited,stack):

visited[v] = Truefor i in self.graph[v]:

if visited[i] == False:self.topologicalSortUtil(i,visited,stack)

stack.insert(0,v)def topologicalSort(self):

visited = [False] * self.Vstack =[]for i in range(self.V):

if visited[i] == False:self.topologicalSortUtil(i,visited,stack)

print(stack)

iii. Find the topological ordering for the given graph. (4)

Topological Ordering:B , A, D, C, E

[Or]

b. i. Write Dijkstra’s Algorithm to find the Shortest path. (3)def dijkstra(self, src):

dist = [sys.maxsize] * self.Vdist[src] = 0sptSet = [False] * self.Vfor cout in range(self.V):

u = self.minDistance(dist, sptSet)sptSet[u] = Truefor v in range(self.V):

if self.graph[u][v] > 0 and sptSet[v] == False anddist[v] > dist[u] + self.graph[u][v]:dist[v] = dist[u] + self.graph[u][v]

self.printSolution(dist)

ii. Find the Shortest path for all the vertex by considering source vertex as 5. (7)

VERTEX DISTANCE1 62 33 34 25 0

Part A [4 x 2 = 8 Marks]5.1 Define Hashing and its types.

Hashing is the transformation of a string of characters into a shorter fixed length value or key that represents the original string. [1 mark]

Types of Hashing: Static and Dynamic [1 mark]

5.2 Write the Analysis of Merge sort and Insertion sort.MergeSort: [1 mark]

Best Case: O(N logN)Average Case: O(N logN)Worst Case: O(N logN)

Insertion Sort: [1 mark]Best Case: O(N)Average Case: O(N2)Worst Case: O(N2)

5.3 List various Collision resolution techniques. [2 mark]Separate ChainingOpen Addressing – Linear Probing, Quadratic Probing, Double hashingRehashingExtendible hashing

5.4 Define Sorting and its types.Sorting is the process of arranging elements in a collection in some order. Types of sorting are

Internal sorting and External sorting [1 mark]External Sorting: Sorting takes place in the secondary memory of a computerInternal Sorting: Sorting technique which require the dataset to be in the main memory [1 mark]

Part B [1x7=7 Marks] Question No.5.5 is Compulsory

5.5 Write python code to implement Bubble Sort. (4)def bubbleSort(a):

for i in range (0,len(a)-1):for j in range(0,len(a)-1-i):

if a[j] > a[j+1]:temp = a[j]a[j] = a[j+1]a[j+1] = temp

a = [40,30,20,10]bubbleSort(a)print(“Sorted Values : ”,a)

Write python code to implement Linear Search. (3)def linearSearch(a,key):

for i in range(len(a)):if a[i]==key:

return ireturn -1

a = [40,30,20,10]if linearSearch(a,30):

print(“Element Found”)else:

print(“Element not Found”)

Part C [1x10=10 Marks] 5.6 a. i. Write a python code to implement quick sort. (6)

def quickSort(a,left,right):pivotpos = partition(a,left,right)quickSort(a,left,pivotpos-1)quickSort(a,pivotpos+1,right)

def partition(a,left,right):pivot=a[left]i=left+1j=rightwhile(True):

while i<=j and a[i]<=pivot:i=i+1

while j>=i and a[j]>=pivot:j=j-1

if i<j:a[i],a[j] = a[j], a[i]

else:break

a[left],a[j] = a[j], a[left]return j

a = [40,30,20,10]quickSort(a)print(“Sorted Values : ”,a)

ii. Sort the following numbers using Quicksort. (4)34, 56, 12, 42, 38, 67, 82, 10, 19

[Or] b. i. Write python code to implement binary search with suitable example. (5)

def binarySearch(a,key):first=0last = len(a)-1while first<= last:

mid = (first+last)//2if a[mid] == key:

return midelif a[mid] < key:

first = mid +1else:

last = mid-1return -1

a = [40,30,20,10]if binarySearch(a,30):

print(“Element Found”)else:

print(“Element not Found”) [4 mark]

ii. Explain Separate Chaining with proper example. (5)To handle collisions, the hash table has a technique known as separate chaining. It uses linked

lists to overcome the problem of collision. All the keys that resolve to the same hash values are maintained in a linked list. Each position in the hash table acts as a header node for a linked list.

[2 mark]Insertion: [3 marks]

Traverse down the list to check whether the element is already present in place. If element turns out to be new, it is inserted in the front or end of the listLet us consider a simple hash function as “key mod 7” and sequence of keys as 50, 700, 76, 85, 92, 73, and 101.