Skip to content

Commit

Permalink
acyclic paths
Browse files Browse the repository at this point in the history
  • Loading branch information
brunolnetto committed Jan 13, 2022
1 parent 5731932 commit e61d786
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
34 changes: 4 additions & 30 deletions src/data-structures/graph/Graph.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getAllIndexes from '../../utils/arrays/arrays.js'
import { cyclicSort, getAllIndexes } from '../../utils/arrays/arrays.js'

export default class Graph {
#cycles;
Expand Down Expand Up @@ -434,6 +434,7 @@ export default class Graph {
allCycles(){
let marked = [];
let marked_stack = [];
this.#cycles = [];
let n_vertices = this.getNumVertices();

for(let i=0; i<n_vertices; i++){
Expand All @@ -453,35 +454,8 @@ export default class Graph {

return this.#cycles;
}

/**
* @param {GraphVertex} from
* @param {GraphVertex} to
* @return {Array[Integer]} paths
*/
allPaths(from, to){
let from_index = this.getVertexIndex(from);
let to_index = this.getVertexIndex(to);
let n_vertices = this.getNumVertices();

let is_visited = new Array(this.v);
for(let i=0; i<n_vertices; i++) {
is_visited[i]=false;
}

let path_list = [];
let paths = [];

// add source to path[]
path_list.push(from_index);

// Call recursive utility
this.#allPathsUtil(from_index, to_index, is_visited, path_list, paths);

return paths;
}

#allPathsUtil(from_index, to_index, is_visited, local_path_list, paths) {
#recurAcyclicPaths(from_index, to_index, is_visited, local_path_list, paths) {
let adj_list = this.getAdjacencyList();
let adj_len = adj_list[from_index].length;

Expand All @@ -505,7 +479,7 @@ export default class Graph {
// store current node in path[]
local_path_list.push(neighbor_i);

this.#allPathsUtil(neighbor_i, to_index, is_visited,
this.#recurAcyclicPaths(neighbor_i, to_index, is_visited,
local_path_list, paths);

// remove current node in path[]
Expand Down
4 changes: 1 addition & 3 deletions src/data-structures/graph/__test__/Graph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ describe('Graph', () => {
const edgeAB = new GraphEdge(vertexA, vertexB);
const edgeAC = new GraphEdge(vertexA, vertexC);

graph
.addEdge(edgeAB)
.addEdge(edgeAC);
graph.addEdges([edgeAB, edgeAC]);

const neighbors = graph.getNeighbors(vertexA);

Expand Down
3 changes: 3 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ app.get('/', (req, res) => {
let from = A;
let to = D;

console.log(graph_.getVertexCycles());


res.send(graph_.describe());
});
// [END app]
Expand Down

0 comments on commit e61d786

Please sign in to comment.