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....
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?
(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?
Design a nonrecursive post order traversal algorithm. 1.Use the linked representation for the binary tree to...
Design a nonrecursive post order traversal algorithm. 1.Use the linked representation for the binary tree to be traversed. 2.Except left, right, and data fields, there are no other fields in a node. 3.After the traversal the tree should remain intact. 4.Use only one stack. 5.You can’t push anything other than nodes into the stack.
how do I create a graph class to store a adjacency list representation of a graph?...
how do I create a graph class to store a adjacency list representation of a graph? I need some help and dont conpletely understand how to start. if you could provide a code with some steps id appreciate it.
In java what is the best algorithm and data structure choice to search for all instances...
In java what is the best algorithm and data structure choice to search for all instances where a pattern is found some larger texts. Also please provide the worst-case complexity big o notation of this and why it is best. Assuming the texts are all stored in a class instance that has a field contents. Which is then stored as a array list value in a linked hash map with the key being the username of the poster.
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...
write an algorithm function called balance() in javascript that takes in a string and returns a...
write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”
Question The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and...
Question The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and decryption”. Write Java or Python code to implement either Monoalphabetic cipher or Hill cipher or Transposition Cipher (Encryption and Decryption) and test your code on given plaintext. User may enter value of key at the command prompt, if required.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT