Skip to content

Commit 2733b17

Browse files
committed
Initial commit
0 parents  commit 2733b17

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2016 Dexcode, Inc.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Graph Algorithm
2+
3+
This contains graph algorithm implemented in JavaScript. Currently it only has
4+
the following functionality:
5+
6+
- hasLoop: Detect if a loop in a graph and returns the loop.

graph_algorithm.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
(function() {
2+
var WHITE = 0
3+
var GRAY = 1
4+
var BLACK = 2
5+
6+
var GraphAlgorithm = {
7+
//
8+
// Detect loop in a directed graph, use graph coloring algorithm.
9+
//
10+
// @param vertices - array of vertices, a vertex is either an integer or a
11+
// string
12+
// @param edges - array of edges, an edge is an array of length 2 which
13+
// marks the source and destination vertice.
14+
// i.e., [source, dest]
15+
//
16+
// @return { hasLoop: true, loop: [...] }
17+
//
18+
hasLoop: function(vertices, edges) {
19+
var colors = {}
20+
var path = []
21+
22+
// Initialize colors to white
23+
for (var i=0; i<vertices.length; ++i) {
24+
colors[vertices[i]] = WHITE
25+
}
26+
27+
// For all vertices, do DFS traversal
28+
for (var i=0; i<vertices.length; ++i) {
29+
var vertex = vertices[i]
30+
if (colors[vertex] == WHITE) {
31+
var result = this._hasLoopDFS(vertices, edges, colors, path, vertex)
32+
33+
if (result.hasLoop) {
34+
return result
35+
}
36+
}
37+
}
38+
39+
return { hasLoop: false }
40+
},
41+
42+
_hasLoopDFS: function(vertices, edges, colors, path, vertex) {
43+
colors[vertex] = GRAY
44+
path.push(vertex)
45+
46+
var adjacentEdges = []
47+
for (var i=0; i<edges.length; ++i) {
48+
var edge = edges[i]
49+
if (edge[0] == vertex) {
50+
adjacentEdges.push(edge)
51+
}
52+
}
53+
54+
for (var i=0; i<adjacentEdges.length; ++i) {
55+
var edge = adjacentEdges[i]
56+
var adjVertex = edge[1]
57+
58+
if (colors[adjVertex] == GRAY) {
59+
var loop = path.slice(path.indexOf(adjVertex))
60+
return { hasLoop: true, loop: loop }
61+
}
62+
63+
if (colors[adjVertex] == WHITE) {
64+
var result = this._hasLoopDFS(vertices, edges, colors, path, adjVertex)
65+
if (result.hasLoop) {
66+
return result
67+
}
68+
}
69+
}
70+
71+
colors[vertex] = BLACK
72+
path.pop(vertex)
73+
74+
return { hasLoop: false }
75+
}
76+
}
77+
78+
window.GraphAlgorithm = GraphAlgorithm
79+
})()

0 commit comments

Comments
 (0)