|
1 |
| -// create a graph class |
2 |
| -class Graph { |
3 |
| - // defining vertex array and |
4 |
| - // adjacent list |
5 |
| - constructor(noOfVertices) |
6 |
| - { |
7 |
| - this.noOfVertices = noOfVertices; |
8 |
| - this.AdjList = new Map(); |
9 |
| - } |
10 |
| - |
11 |
| - // functions to be implemented |
12 |
| - |
13 |
| - // addVertex(v) |
14 |
| - // addEdge(v, w) |
15 |
| - // printGraph() |
16 |
| - |
17 |
| - // bfs(v) |
18 |
| - // dfs(v) |
19 |
| -} |
| 1 | +// create a graph class |
| 2 | +class Graph { |
| 3 | + // defining vertex array and |
| 4 | + // adjacent list |
| 5 | + constructor (noOfVertices) { |
| 6 | + this.noOfVertices = noOfVertices |
| 7 | + this.AdjList = new Map() |
| 8 | + } |
20 | 9 |
|
21 |
| -// add vertex to the graph |
22 |
| -addVertex(v) |
23 |
| -{ |
24 |
| - // initialize the adjacent list with a |
25 |
| - // null array |
26 |
| - this.AdjList.set(v, []); |
27 |
| -} |
| 10 | + // functions to be implemented |
28 | 11 |
|
29 |
| -// add edge to the graph |
30 |
| -addEdge(v, w) |
31 |
| -{ |
32 |
| - // get the list for vertex v and put the |
33 |
| - // vertex w denoting edge between v and w |
34 |
| - this.AdjList.get(v).push(w); |
35 |
| - |
36 |
| - // Since graph is undirected, |
37 |
| - // add an edge from w to v also |
38 |
| - this.AdjList.get(w).push(v); |
39 |
| -} |
| 12 | + // addVertex(v) |
| 13 | + // addEdge(v, w) |
| 14 | + // printGraph() |
40 | 15 |
|
| 16 | + // bfs(v) |
| 17 | + // dfs(v) |
41 | 18 |
|
42 |
| -// Prints the vertex and adjacency list |
43 |
| -printGraph() |
44 |
| -{ |
45 |
| - // get all the vertices |
46 |
| - var get_keys = this.AdjList.keys(); |
47 |
| - |
48 |
| - // iterate over the vertices |
49 |
| - for (var i of get_keys) |
50 |
| -{ |
51 |
| - // great the corresponding adjacency list |
52 |
| - // for the vertex |
53 |
| - var get_values = this.AdjList.get(i); |
54 |
| - var conc = ""; |
55 |
| - |
56 |
| - // iterate over the adjacency list |
57 |
| - // concatenate the values into a string |
58 |
| - for (var j of get_values) |
59 |
| - conc += j + " "; |
60 |
| - |
61 |
| - // print the vertex and its adjacency list |
62 |
| - console.log(i + " -> " + conc); |
63 |
| - } |
64 |
| -} |
| 19 | + // add vertex to the graph |
| 20 | + addVertex (v) { |
| 21 | + // initialize the adjacent list with a |
| 22 | + // null array |
| 23 | + |
| 24 | + this.AdjList.set(v, []) |
| 25 | + } |
| 26 | + |
| 27 | + // add edge to the graph |
| 28 | + addEdge (v, w) { |
| 29 | + // get the list for vertex v and put the |
| 30 | + // vertex w denoting edge between v and w |
| 31 | + this.AdjList.get(v).push(w) |
| 32 | + |
| 33 | + // Since graph is undirected, |
| 34 | + // add an edge from w to v also |
| 35 | + this.AdjList.get(w).push(v) |
| 36 | + } |
65 | 37 |
|
| 38 | + // Prints the vertex and adjacency list |
| 39 | + printGraph () { |
| 40 | + // get all the vertices |
| 41 | + const getKeys = this.AdjList.keys() |
66 | 42 |
|
| 43 | + // iterate over the vertices |
| 44 | + for (const i of getKeys) { |
| 45 | + // great the corresponding adjacency list |
| 46 | + // for the vertex |
| 47 | + const getValues = this.AdjList.get(i) |
| 48 | + let conc = '' |
| 49 | + |
| 50 | + // iterate over the adjacency list |
| 51 | + // concatenate the values into a string |
| 52 | + for (const j of getValues) { |
| 53 | + conc += j + ' ' |
| 54 | + } |
| 55 | + |
| 56 | + // print the vertex and its adjacency list |
| 57 | + console.log(i + ' -> ' + conc) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
67 | 61 | // Example
|
68 |
| -var graph = new Graph(6); |
69 |
| -var vertices = [ 'A', 'B', 'C', 'D', 'E', 'F' ]; |
70 |
| - |
71 |
| -// adding vertices |
72 |
| -for (var i = 0; i < vertices.length; i++) { |
73 |
| - g.addVertex(vertices[i]); |
74 |
| -} |
75 |
| - |
76 |
| -// adding edges |
77 |
| -g.addEdge('A', 'B'); |
78 |
| -g.addEdge('A', 'D'); |
79 |
| -g.addEdge('A', 'E'); |
80 |
| -g.addEdge('B', 'C'); |
81 |
| -g.addEdge('D', 'E'); |
82 |
| -g.addEdge('E', 'F'); |
83 |
| -g.addEdge('E', 'C'); |
84 |
| -g.addEdge('C', 'F'); |
85 |
| - |
86 |
| -// prints all vertex and |
87 |
| -// its adjacency list |
88 |
| -// A -> B D E |
89 |
| -// B -> A C |
90 |
| -// C -> B E F |
91 |
| -// D -> A E |
92 |
| -// E -> A D F C |
93 |
| -// F -> E C |
94 |
| -g.printGraph(); |
| 62 | +const graph = new Graph(6) |
| 63 | +const vertices = ['A', 'B', 'C', 'D', 'E', 'F'] |
| 64 | + |
| 65 | +// adding vertices |
| 66 | +for (let i = 0; i < vertices.length; i++) { |
| 67 | + graph.addVertex(vertices[i]) |
| 68 | +} |
| 69 | + |
| 70 | +// adding edges |
| 71 | +graph.addEdge('A', 'B') |
| 72 | +graph.addEdge('A', 'D') |
| 73 | +graph.addEdge('A', 'E') |
| 74 | +graph.addEdge('B', 'C') |
| 75 | +graph.addEdge('D', 'E') |
| 76 | +graph.addEdge('E', 'F') |
| 77 | +graph.addEdge('E', 'C') |
| 78 | +graph.addEdge('C', 'F') |
95 | 79 |
|
| 80 | +// prints all vertex and |
| 81 | +// its adjacency list |
| 82 | +// A -> B D E |
| 83 | +// B -> A C |
| 84 | +// C -> B E F |
| 85 | +// D -> A E |
| 86 | +// E -> A D F C |
| 87 | +// F -> E C |
| 88 | +graph.printGraph() |
0 commit comments