Question

In: Computer Science

This question is related to graph representation in Data Structure and Algorithm in Javascript. Question 1-...

This question is related to graph representation in Data Structure and Algorithm in Javascript.

Question 1-

Write a code in javascript in which you have to implement a function that converts

adjacency matrix to adjacency list-store and it should have following signature

function convertToAdjList( adjMatrix);

You also have to include the functions that can demonstrate your implementation work with few inputs. Submit your javascript code with output as well.

Question 2

Tell the runtime complexity of the conversion that implemented. Is it depend on the number of edges or number of vertices or both? What

should be the runtime complexity of a conversion function from

adjacency list-store to matrix be? Give the brief reasoning with some examples.

Solutions

Expert Solution

function convertToAdjList(adjMatrix)
{
   var adjList=[];
   for(var i=0;i<adjMatrix.length;i++)
   {
     var arr=[];
     for(var j=0;j<adjMatrix.length;j++)
     {
       if(adjMatrix[i][j]==1)
       {
        arr.push(j);
       }
     }
     adjList[i]=arr;
   }
   return adjList;
}
var A1=[[0,0,1],[0,0,1],[1,1,0]];
var A2=[[0,1,1],[1,0,0],[1,0,0]];
var A3=[[0,1,0],[1,0,1],[0,1,0]];
console.log(convertToAdjList(A1));
console.log(convertToAdjList(A1));
console.log(convertToAdjList(A1));

Here, the time complexity of this program is dependent on number of vertcies. Since, two for loops are runnning to iterate through the vertices so, its time complexity is O(V^2) where, V is number of vertices in the graph. This code goes through the whole matrix and checks if the value is 1 and when this condition becomes true, it will push the corresponding column number(j) to arr for corresponding ith vertex.For e.g in matrix A1 there is edge between 0 and 2 vertex and 1 and 2 vertex. So, For i=0 it will store 2 in array as is neighbour and for i=1 it will store 2 as its neighbour. And for i=2 as 0 and 1 both are its neighbour it will store 0,1 in arr as its neighbour. And thus, as it have to iterate through matrix of size V^2 to check whether particular pair are neighbours or not time complexity becomes V^2.

To convert adjancy list to matrix the time complexity would be O(V*E) where, E is number of edges. As we would have to go though each vertex and all of its corresponding edges.


Related Solutions

Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly...
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly linked list in ascending order. Why is this a bad idea? How does insertion sort’s runtime complexity change if you were to use it to sort a linked list? 2. Your classmate thinks that input arrays to the merge operation of merge sort don’t need to be in sorted order. Is your classmate right or wrong? Why?
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary...
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary data structure using the prototype. Run some tests that show that your code works. 2. Implement the hash table data structure using the prototypeRun some tests that show that your code works. 3. The book discusses linear probing, but their approach has a serious problem. What is the issue? 4. Complete the method below that adds all key-value pairs from one dictionary into another....
(1) Run Prim's algorithm and Kruskal's algorithm for the following graph: G(V, E) where V =...
(1) Run Prim's algorithm and Kruskal's algorithm for the following graph: G(V, E) where V = {A, B, C, D, E} E = { {A,B}, {B,C}, {C, A}. {B, D}, {B, E}, {D, E}} use edge weights: w({A, B} = 1), w({B, C}) = 2, w({C, A}) = 3, w({B, D}) = 4, w({B, E}) = 5, w({D, E}) = 6. (2) Can the maximum weight edge be in every minimum spanning tree of a graph?
This question is related to treasury bonds’ term structure. a. “A term structure of bond yields...
This question is related to treasury bonds’ term structure. a. “A term structure of bond yields tells us how bond yields change over time.” Explain whether you agree or disagree with the statement. b. When you observe a down-sloping yield curve, what may you say about the economy under the liquidity premium theory?
Course name : Algorithm and Data Structure-I Write a brief note about dependent structure. Compare between...
Course name : Algorithm and Data Structure-I Write a brief note about dependent structure. Compare between tree and qraph. Explain What we means by "Data encapsulation"? And give example. Explain the concept of interface in java with example. Calculate T(n) and Big O notation for the following algorithm:     Initialize sum to 0. Initialize index variable i to 0 While i<n do the following Add x[i] to sum.   Increment i by 1.           Calculate and return mean = sum / n...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that can sort a singly-linked-list with Merge-sort. The program should read integers from file (hw-extra.txt) and create an unsorted singly-linked list. Then, the list should be sorted using merge sort algorithm. The merge-sort function should take the head of a linked list, and the size of the linked list as parameters. hw-extra.txt provided: 37 32 96 2 25 71 432 132 76 243 6 32...
Give an algorithm to detect whether a given undirected graph contains a cycle. If the graph...
Give an algorithm to detect whether a given undirected graph contains a cycle. If the graph contains a cycle, then your algorithm should output one. (It should not output all cycles in the graph, just one of them.) The running time of your algorithm should be O(m + n) for a graph with n nodes and m edges.
Question 2 Required Discuss generally why accountants need to understand data representation, basic data structures and...
Question 2 Required Discuss generally why accountants need to understand data representation, basic data structures and coding schemes.  
Required Question:  Analyze the Article related to Elasticity of Demand in UAE The use of graph and...
Required Question:  Analyze the Article related to Elasticity of Demand in UAE The use of graph and diagrams are preferred to explain theories. . Read the Article UAE's food and beverage market forecast to remain healthy Spending by residents on the food and beverage (F&B) will increase over the next five years, mainly due to increase in consumer base and growth in the income, according to industry analysts. Analysts believe that the impact of the VAT will be limited but impact...
One simple representation for a graph is to use an adjacency matrix: each of the N...
One simple representation for a graph is to use an adjacency matrix: each of the N nodes is given a unique number in the range 0 to N-1 to identify it. A large two dimensional array A with N rows and N columns is created so that A[x][y] stores the cost of travelling directly from node x to node y. if A[x][y] is zero, then there is no direct connection from x to y. A[x][y] does not need to equal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT