Skip to content

Commit

Permalink
reachibility object
Browse files Browse the repository at this point in the history
  • Loading branch information
brunolnetto committed Jan 8, 2022
1 parent 6e2bbd5 commit aa6ccaa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
13 changes: 10 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// [START app]
const express = require('express');
const { Graph, addEdge, AllPaths } = require('./utils/directed_graph.js');
const { Graph, addEdge, AllPaths, AllVicinity } = require('./utils/directed_graph.js');

const app = express();

Expand Down Expand Up @@ -32,13 +32,20 @@ app.get('/', (req, res) => {
addEdge(1, 3);

// arbitrary source
let s = 2;
let s = 3;

// arbitrary destination
let d = 3;
let d = 1;

const all_paths = AllPaths(s, d);
const all_vicinity = AllVicinity();

console.log('Paths from '+s+' to '+d+' :');
console.log(all_paths);

console.log('Reachibility: ');
console.log(all_vicinity);

let text_ = "Graph image: <a href=https://www.geeksforgeeks.org/find-paths-given-source-destination//> Graph paths original post </a> " + "<br>";
text_ = text_ + "Following are all different paths from " + s + " to " + d + "<Br>";
for (let i=0; i< all_paths.length; i++) {
Expand Down
27 changes: 25 additions & 2 deletions utils/directed_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,29 @@ function initAdjList(){
// add edge from u to v
function addEdge(u,v){
// Add v to u's list.
adjList[u].push(v);
adjList[u].push(v);
}

function AllVicinity(s, d){
let paths = {};

// Initialization
for(let i=0; i<v; i++){
paths[i] = [];
}

//
for(let i=0; i<v; i++){
for(let j=0; j<v; j++){
let paths_ij = AllPaths(i, j)

if(paths_ij.length!=0){
paths[i].push(j);
}
}
}

return paths;
}

// Prints all paths from 's' to 'd'
Expand Down Expand Up @@ -83,5 +105,6 @@ function AllPathsUtil(u, d, isVisited, localPathList, paths) {
module.exports = {
Graph: (num_vertices) => Graph(num_vertices),
addEdge: (source, destination) => addEdge(source, destination),
AllPaths: (source, destination) => AllPaths(source, destination)
AllPaths: (source, destination) => AllPaths(source, destination),
AllVicinity: () => AllVicinity()
};

0 comments on commit aa6ccaa

Please sign in to comment.