Skip to content

Commit

Permalink
fix/ fix getVertex by index and allPaths from A to B
Browse files Browse the repository at this point in the history
  • Loading branch information
brunolnetto committed Feb 13, 2022
1 parent 28d028c commit 6066a09
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
14 changes: 5 additions & 9 deletions data-structures/graph/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import stronglyConnectedComponents from '../../algorithms/strongly-connected-com
import eulerianPath from '../../algorithms/eulerian-path/eulerianPath.js';
import depthFirstSearch from '../../algorithms/depth-first-search/depthFirstSearch.js';
import VisitMetadata from './VisitMetadata.js';
import { arraysEqual, extendedVenn, removeArrayDuplicates } from '../../utils/arrays/arrays.js'
import { cartesianProduct,
extendedVenn,
removeArrayDuplicates } from '../../utils/arrays/arrays.js'

export default class Graph {
#cycles;
Expand Down Expand Up @@ -91,7 +93,7 @@ export default class Graph {
*/
getVertexByIndex(vertexIndex) {
let indices_to_vertices = this.getIndicesToVertices();
return indices_to_vertices[vertexIndex];
return this.vertices[indices_to_vertices[vertexIndex]];
}

/**
Expand Down Expand Up @@ -1042,7 +1044,6 @@ export default class Graph {
**/
acyclicPaths(from, to) {
const verticesIndices = this.getVerticesIndices();

const from_index = verticesIndices[from];
const to_index = verticesIndices[to];

Expand Down Expand Up @@ -1073,12 +1074,7 @@ export default class Graph {
let outflow_nodes=[];
let new_routes=[];
let only_cycle_nodes=_.difference(cycle_nodes, acyclic_path);
let all_nodes=_.union(cycle_nodes, acyclic_path);

// Cartesian product of arrays
let f = (a, b) => [].concat(...a.map(a => b.map(b => [].concat(a, b))));
let cartesian = (a, b, ...c) => b ? cartesian(f(a, b), ...c) : a;

// Cycles intercept the main path
for(let intersect_node of intersect_nodes){
if(_.intersection(forward_star[intersect_node], only_cycle_nodes).length!=0){
Expand All @@ -1092,7 +1088,7 @@ export default class Graph {

// New routes comes from outflow-inflow cycles
let new_route=[]
for(let combination of cartesian(outflow_nodes, inflow_nodes)){
for(let combination of cartesianProduct(outflow_nodes, inflow_nodes)){
let start_node_id=combination[0];
let finish_node_id=combination[1];

Expand Down
2 changes: 1 addition & 1 deletion data-structures/graph/__test__/Graph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ describe('Graph', () => {
// Nodes
const node_labels = ['A', 'B', 'C', 'D', 'E', 'F'];
const [A, B, C, D, E, F] = createVertices(node_labels);

// Vertices
const edge_vertices = [[A, B], [B, C], [C, D], [C, E], [E, B], [C, F], [F, B]];

Expand Down
16 changes: 15 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
fromStartToFinishCombsAcyclicPaths,
} from './utils/workflow/parsers.js';

import { cartesianProduct } from './utils/arrays/arrays.js'

const require = createRequire(import.meta.url);
const app = express();

Expand Down Expand Up @@ -53,7 +55,19 @@ app.get('/', (req, res) => {
const graph = parseBlueprintToGraph(blueprint_i);
const route_describe = fromStartToFinishCombsAcyclicPaths(blueprint_i);

res.send(graph.allPaths());
let start_nodes_indexes=graph.orphanNodes();
let finish_nodes_indexes=graph.looseNodes();

let paths = [];
for(let [start_node_index,
finish_node_index] of cartesianProduct(start_nodes_indexes, finish_nodes_indexes)){
let start_node = graph.getVertexByIndex(start_node_index)
let finish_node = graph.getVertexByIndex(finish_node_index)

paths.push(graph.allPaths(start_node, finish_node))
}

res.send(paths);
}

});
Expand Down
9 changes: 8 additions & 1 deletion utils/arrays/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,19 @@ export const arraysEqual = (a, b) => {
return true;
}

// Cartesian product of arrays
export const cartesianProduct = (a, b, ...c) => {
let f = (a, b) => [].concat(...a.map(a => b.map(b => [].concat(a, b))));

return b ? cartesianProduct(f(a, b), ...c) : a;
}

export const removeArrayDuplicates = (list) => {
let unique = [];

list.forEach((item) => {
let has_item=false

unique.forEach((unique_item) => {
has_item=has_item || arraysEqual(item, unique_item)
})
Expand Down

0 comments on commit 6066a09

Please sign in to comment.