1

I was building a graph in JavaScript and I met this problem.

The output is not what I want.

What I know about graphs is between each node has edge and this adjacentList's goal is to show the edge and make a connection to the node.

If my code is not complete, please give me a hint to finish it or just fix my code and meet the request.

The output should be:

Answer:
0-->1 2
1-->3 2 0
2-->4 1 0
3-->1 4
4-->3 2 5
5-->4 6
6-->5

Here is my JS:

 class Graph { 
    constructor() { 
    this.numberOfNodes = 0;
    this.adjacentList = {
    }; 
  } 
  addVertex(node)  { 
    this.adjacentList[node]=[];
    this.numberOfNodes++;
   } 
   addEdge(node1, node2) { 
    this.adjacentList[node1]=[];
    this.adjacentList[node1].push(node2);
   } 
  showConnections() { 
    const allNodes = Object.keys(this.adjacentList); 
    for (let node of allNodes) { 
       let nodeConnections = this.adjacentList[node]; 
       let connections = ""; 
       let vertex;
    for (vertex of nodeConnections) {
        connections += vertex + " ";
      } 
       console.log(node + "-->" + connections); 
      } 
     } 
    } 

    const myGraph = new Graph();
    myGraph.addVertex('0');
    myGraph.addVertex('1');
    myGraph.addVertex('2');
    myGraph.addVertex('3');
    myGraph.addVertex('4');
    myGraph.addVertex('5');
    myGraph.addVertex('6');
    myGraph.addEdge('3', '1'); 
    myGraph.addEdge('3', '4'); 
    myGraph.addEdge('4', '2'); 
    myGraph.addEdge('4', '5'); 
    myGraph.addEdge('1', '2'); 
    myGraph.addEdge('1', '0'); 
    myGraph.addEdge('0', '2'); 
    myGraph.addEdge('6', '5');

    myGraph.showConnections(); 

2
  • Maybe try one of these references: google.com/search?q=js+graph+structure Commented Mar 11, 2019 at 4:51
  • @cat thanks,but i want to use my code to get the output because it's my homework Commented Mar 11, 2019 at 5:40

3 Answers 3

1

I came up with Map and Set to solve the problem. You can take a look.

function Graph() {
  let nodes = new Map()
  
  this.addVertex = node => {
    if(!nodes.has(node)) nodes.set(node, new Set())
  }
  
  this.addEdge = (node, egde) => {
    if(nodes.has(node) && nodes.has(egde)) {
      nodes.get(node).add(egde)
      nodes.get(egde).add(node)
    }
  }
  
  this.showConnections = () => {
    nodes.forEach((node, index) => console.log(`${index} -> ${[...node]}`) )
  }
}

const myGraph  = new Graph()
    myGraph.addVertex('0');
    myGraph.addVertex('1');
    myGraph.addVertex('2');
    myGraph.addVertex('3');
    myGraph.addVertex('4');
    myGraph.addVertex('5');
    myGraph.addVertex('6');
    myGraph.addEdge('3', '1'); 
    myGraph.addEdge('3', '4'); 
    myGraph.addEdge('4', '2'); 
    myGraph.addEdge('4', '5'); 
    myGraph.addEdge('1', '2'); 
    myGraph.addEdge('1', '0'); 
    myGraph.addEdge('0', '2'); 
    myGraph.addEdge('6', '5');

myGraph.showConnections();

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks,but i want to use my code to get the answer.Is there something i can do in my code?
It's really hard to understand your code. What numberOfNodes is using for? You create newNode but didn't use it (in both functions). And the way you're working with array and object is so verbose(showConnections function). You should read more about how to work with these objects.
I posted a new answer for that. Check it out.
0
addVertex(node)  { 
  this.adjacentList[node]=[]; <---| 
  this.numberOfNodes++;           |
 }                                |  <-- Do the same task
 addEdge(node1, node2) {          |  
  this.adjacentList[node1]=[];----|
  this.adjacentList[node1].push(node2);
} 

Then you should remove this.adjacentList[node1]=[] from addEdge function. And because your graph is getting data from both sides:

[{'1': '3'}, {'3': '2'}] =>  3-->1,2 

Then you should push the node in 2 ways

addEdge(node1, node2) {
  this.adjacentList[node1].push(node2);
  this.adjacentList[node2].push(node1);
}

The code should be:

class Graph { 
    constructor() { 
    this.numberOfNodes = 0;
    this.adjacentList = {
    }; 
  } 
  addVertex(node)  { 
    this.adjacentList[node]=[];
    this.numberOfNodes++;
   } 
   addEdge(node1, node2) { 
    // this.adjacentList[node1]=[]; <------------ Remove this line
     this.adjacentList[node1].push(node2);
     this.adjacentList[node2].push(node1); // <-- Add this line
   }
  
  showConnections() { 
    const allNodes = Object.keys(this.adjacentList); 
    for (let node of allNodes) { 
       let nodeConnections = this.adjacentList[node]; 
       let connections = ""; 
       let vertex;
    for (vertex of nodeConnections) {
        connections += vertex + " ";
      } 
       console.log(node + "-->" + connections); 
      } 
     } 
    } 

    const myGraph = new Graph();
    myGraph.addVertex('0');
    myGraph.addVertex('1');
    myGraph.addVertex('2');
    myGraph.addVertex('3');
    myGraph.addVertex('4');
    myGraph.addVertex('5');
    myGraph.addVertex('6');
    myGraph.addEdge('3', '1'); 
    myGraph.addEdge('3', '4'); 
    myGraph.addEdge('4', '2'); 
    myGraph.addEdge('4', '5'); 
    myGraph.addEdge('1', '2'); 
    myGraph.addEdge('1', '0'); 
    myGraph.addEdge('0', '2'); 
    myGraph.addEdge('6', '5');

    myGraph.showConnections(); 

Besides, you should handle the error and take a look at showConnection to optimate it.

1 Comment

Thanks for your answer!
0

Object properties are defined using a semicolon ' rather than equals =. Also, individual properties are separated using comma , rather than semi-colon ;

So your snippet:

const newNode={
      this.numberOfNodes = 0;
      this.adjacentList = {
     };

should actually be something like:

const newNode={
      numberOfNodes : 0,
      adjacentList : {
     }; 

1 Comment

Fixing all this still gives node is not defined because you are referencing node inside addEdge() where the former is not defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.