FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Data-Structures/Graph/Graph2.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Data-Structures
/
Graph
/
Graph2.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (50 loc) · 1.32 KB
Breadcrumbs
JavaScript
/
Data-Structures
/
Graph
/
Graph2.js
Copy path
File metadata and controls
62 lines (50 loc) · 1.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// create a graph class
class
Graph
{
// defining vertex array and
// adjacent list
constructor
(
noOfVertices
)
{
this
.
noOfVertices
=
noOfVertices
this
.
AdjList
=
new
Map
(
)
}
// functions to be implemented
// addVertex(v)
// addEdge(v, w)
// printGraph()
// bfs(v)
// dfs(v)
// add vertex to the graph
addVertex
(
v
)
{
// initialize the adjacent list with a
// null array
this
.
AdjList
.
set
(
v
,
[
]
)
}
// add edge to the graph
addEdge
(
v
,
w
)
{
// get the list for vertex v and put the
// vertex w denoting edge between v and w
this
.
AdjList
.
get
(
v
)
.
push
(
w
)
// Since graph is undirected,
// add an edge from w to v also
this
.
AdjList
.
get
(
w
)
.
push
(
v
)
}
// Prints the vertex and adjacency list
printGraph
(
output
=
(
value
)
=>
console
.
log
(
value
)
)
{
// get all the vertices
const
getKeys
=
this
.
AdjList
.
keys
(
)
// iterate over the vertices
for
(
const
i
of
getKeys
)
{
// get the corresponding adjacency list
// for the vertex
const
getValues
=
this
.
AdjList
.
get
(
i
)
let
conc
=
''
// iterate over the adjacency list
// concatenate the values into a string
for
(
const
j
of
getValues
)
{
conc
+=
j
+
' '
}
// print the vertex and its adjacency list
output
(
i
+
' -> '
+
conc
)
}
}
}
export
{
Graph
}
Back
|
FazBrowse Home
|
New Git URL