forked from marcelklehr/toposort
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert to Typescript ES2020 and fix exports
- Loading branch information
1 parent
0fbc0ae
commit a6d97ed
Showing
19 changed files
with
768 additions
and
482 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
export function makeOutgoingEdges(arr: unknown[][]) { | ||
return arr.reduce((edges, [from, to]) => { | ||
edges.has(from) || edges.set(from, new Set()); | ||
edges.has(to) || edges.set(to, new Set()); | ||
edges.get(from).add(to); | ||
return edges; | ||
}, new Map()); | ||
} | ||
|
||
export function makeIncomingEdges(arr: unknown[][]) { | ||
return arr.reduce((edges, [from, to]) => { | ||
edges.has(from) || edges.set(from, new Set()); | ||
edges.has(to) || edges.set(to, new Set()); | ||
edges.get(to).add(from); | ||
return edges; | ||
}, new Map()); | ||
} | ||
|
||
export function getStartNodes(edges: unknown[][]) { | ||
const incomingEdges = makeIncomingEdges(edges); | ||
const startNodes: unknown[] = []; | ||
incomingEdges.forEach( | ||
(value, key) => value.size === 0 && startNodes.push(key) | ||
); | ||
|
||
return startNodes; | ||
} | ||
|
||
export function makeNodesHash(arr: unknown[]) { | ||
return new Map(arr.map((item, i) => [item, i])); | ||
} | ||
|
||
export function uniqueNodes(arr: unknown[][]) { | ||
const res = new Set(); | ||
for (let i = 0, len = arr.length; i < len; i++) { | ||
const edge = arr[i]; | ||
res.add(edge[0]); | ||
res.add(edge[1]); | ||
} | ||
return [...res]; | ||
} | ||
|
||
export function visitDepthFirst({ | ||
node, | ||
adjacencyMap, | ||
}: { | ||
node: unknown; | ||
adjacencyMap: Map<any, any>; | ||
}) { | ||
const visited = new Set(); | ||
const stack = [node]; | ||
let cur = node; | ||
while (cur) { | ||
visited.add(cur); | ||
const neighbors = [...adjacencyMap.get(cur)]; | ||
stack.push(...neighbors.filter((item) => !visited.has(item)).reverse()); | ||
cur = stack.pop(); | ||
} | ||
return visited; | ||
} | ||
|
||
export function getAdjacencyMapOfIndirectedGraph(edges: unknown[][]) { | ||
return edges.reduce((acc, [from, to]) => { | ||
[ | ||
[from, to], | ||
[to, from], | ||
].forEach(([node, neighbor]) => { | ||
const neighbors = acc.get(node); | ||
if (neighbors) { | ||
neighbors.add(neighbor); | ||
} else { | ||
acc.set(node, new Set([neighbor])); | ||
} | ||
}); | ||
return acc; | ||
}, new Map()); | ||
} | ||
|
||
export function groupByComponents({ edges }: { edges: unknown[][] }) { | ||
const adjacencyMap: Map<any, any> = getAdjacencyMapOfIndirectedGraph(edges); | ||
const nodes = uniqueNodes(edges); | ||
const components = []; | ||
const visitedNodes = new Set(); | ||
let currentNode = nodes[0]; | ||
|
||
while (visitedNodes.size < nodes.length) { | ||
const visited = visitDepthFirst({ adjacencyMap, node: currentNode }); | ||
components.push(visited); | ||
visited.forEach((node) => visitedNodes.add(node)); | ||
currentNode = nodes.find((node) => !visitedNodes.has(node)); | ||
} | ||
|
||
return components; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./extra.js"; | ||
export * from "./toposort.js"; | ||
|
||
export {toposortDefault as default} from "./toposort.js"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.