Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 54 additions & 32 deletions src/DependencyGraph/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export interface TopSortResult<T> {
sorted: T[], cycled: T[],
}

enum NodeVisitStatus {
ON_STACK,
PROCESSED,
POPPED,
}

/**
* Provides graph directed structure
*
Expand Down Expand Up @@ -188,51 +194,67 @@ export class Graph<T> {
const entranceTime: Map<T, number> = new Map()
const low: Map<T, number> = new Map()
const parent: Map<T, T | null> = new Map()
const processed: Set<T> = new Set()
const onStack: Set<T> = new Set()

// node status life cycle:
// undefined -> ON_STACK -> PROCESSED -> POPPED
const nodeStatus: Map<T, NodeVisitStatus> = new Map()
const order: T[] = []

let time: number = 0

modifiedNodes.reverse().forEach( (v: T) => {
if (processed.has(v)) {
if (nodeStatus.get(v) !== undefined) {
return
}
entranceTime.set(v, time)
// order.push(v)
low.set(v, time)
parent.set(v, null)
time++
const DFSstack: T[] = [v]
onStack.add(v)
nodeStatus.set(v, NodeVisitStatus.ON_STACK)
while ( DFSstack.length > 0 ) {
const u = DFSstack[ DFSstack.length - 1 ]
if ( processed.has(u) ) { // leaving this DFS subtree
const pu = parent.get(u)
if ( pu !== null ) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
low.set(pu!, Math.min(low.get(pu!)!, low.get(u)!))
}
DFSstack.pop()
onStack.delete(u)
order.push(u)
} else {
processed.add(u)
entranceTime.set(u, time)
low.set(u, time)
this.adjacentNodes(u).forEach( (t: T) => {
if (entranceTime.get(t) !== undefined) { // forward edge or backward edge
if (onStack.has(t)) { // backward edge
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
low.set(u, Math.min(low.get(u)!, entranceTime.get(t)!))
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
switch(nodeStatus.get(u)!) {
case NodeVisitStatus.ON_STACK: {
nodeStatus.set(u, NodeVisitStatus.PROCESSED)
entranceTime.set(u, time)
low.set(u, time)
this.adjacentNodes(u).forEach( (t: T) => {
switch(nodeStatus.get(t)){
case NodeVisitStatus.POPPED:
break
case NodeVisitStatus.PROCESSED: { //backward edge
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
low.set(u, Math.min(low.get(u)!, entranceTime.get(t)!))
break
}
case undefined: // not visited
// process as in the case of ON_STACK
// eslint-disable-next-line no-fallthrough
case NodeVisitStatus.ON_STACK: { // or visited but not processed
parent.set(t, u)
DFSstack.push(t)
nodeStatus.set(t, NodeVisitStatus.ON_STACK)
time++
break
}
}
} else {
parent.set(t, u)
DFSstack.push(t)
onStack.add(t)
time++
})
break
}
case NodeVisitStatus.PROCESSED: { // leaving this DFS subtree
const pu = parent.get(u)
if ( pu !== null ) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
low.set(pu!, Math.min(low.get(pu!)!, low.get(u)!))
}
})
nodeStatus.set(u, NodeVisitStatus.POPPED)
order.push(u)
DFSstack.pop()
break
}
case NodeVisitStatus.POPPED: { // it's a 'shadow' copy, we already processed this vertex and can ignore it
DFSstack.pop()
break
}
}
}
})
Expand Down
26 changes: 26 additions & 0 deletions test/engine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,29 @@ describe('dateTime', () => {
expect(engine.numberToTime(1.1)).toEqual({'hours': 26, 'minutes': 24, 'seconds': 0})
})
})

describe('engine should build correctly', () => {
it('should build correctly #1', () => {
Comment thread
izulin marked this conversation as resolved.
Resolved
expect(() => HyperFormula.buildFromArray([
['=A3+A2'],
['=A3'],
])).not.toThrowError()
})

it('should build correctly #2', () => {
expect(() => HyperFormula.buildFromArray([
['=A4+A3+A2'],
['=A4+A3'],
['=A4'],
])).not.toThrowError()
})

it('should build correctly #3', () => {
expect(() => HyperFormula.buildFromArray([
['=A5+A4+A3+A2'],
['=A5+A4+A3'],
['=A5+A4'],
['=A5'],
])).not.toThrowError()
})
})