[SOLVED] CSF211 Lab 12-C on Linux

30.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon Lab_12-vnoj7l.zip (539.8 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
🔒 Securely Powered by:
Secure Checkout
5/5 - (1 vote)

 

Programming Environment: C on Linux

Problem-1: Read the following problem and its possible solution. Since this is the last lab of the course and first lab on graphs, see the solution to understand how graphs are implemented. Then, go to implement Problem-2.

Consider a graph G = (V, E, w) – where w is an integer function on V – stored as an adjacency list. Write a best-first traversal procedure: starting at the root, pick the next vertex to be visited as one with the highest weight among the children of all vertices already visited. Given count of vertices, V, create an empty graph with adjacency list representation. The directed graph will be supplied as edge list, where each edge is represented by a pair of vertices which are integers in the range [0, V). Another function will then supply the weights to all the vertices of the graph.

Complete definition of following functions in graph.c. Write any additional functions and data structures in extras.c and extras.h. Consider driver.c as the driver program.

  1. a)  Graph initGraph(int V)

    Return an empty V-vertices adjacency list with all relevant fields properly initialized.

  2. b)  void printAdjacencyList(Graph g)

    Print data of each vertex in a line, with following format:
    <vertex id> <tab> ==> <adjacent vertex id 1> <adjacent vertex id 2>

  3. c)  void insertEdge(Graph g, unsigned int u, unsigned int v)

    Add an edge in the graph between u and v.

  4. d)  void bestFirstTraverse(Graph g)

    Traverse and print the graph in best first traversal order.

[Hint: You may implement a priority queue in extras.c and extras.h to identify the best adjacent neighbour to pick for expansion during traversal.]

Sample Test Case:

Input: Output: 81 65 39 89 61 85

15042018 10

24 25 87 50 79 95 82 98 60 37 84 27 59 02

Weights: 70
Adjacency List:
  1. 0  (70) ==> 2
  2. 1  (90) ==>
  3. 2  (70) ==> 7
  4. 3  (40)==>7
  5. 4  (90) ==>
  6. 5  (60)==>9
  7. 6  (30) ==> 0
  8. 7  (80) ==> 9
  9. 8  (10) ==> 4
  10. 9  (20)==>8

90 70

40 90

60 30 80 10 20

5 4 9

0
1 5

2 7591 5

Best First Traversal: 0213794568

Problem-2: Depth First Search for a Graph

Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree. The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array.

For example, in the following graph, we start traversal from vertex 2. When we come to vertex 0, we look for all adjacent vertices of it. 2 is also an adjacent vertex of 0. If we don’t mark visited vertices, then 2 will be processed again and it will become a non-terminating process. A Depth First Traversal of the following graph is 2, 0, 1, 3.

Store the graph as an adjacency list representation and find its DFS. Modularize the code yourself.

  • Lab_12-vnoj7l.zip