Skip to content

Commit

Permalink
test: 有向图 单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
originalix committed Apr 6, 2021
1 parent f158bb0 commit c156929
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module.exports = {
semi: false,
singleQuote: true,
printWidth: 120,
tabWidth: 2
tabWidth: 2,
trailingComma: 'none'
};
46 changes: 46 additions & 0 deletions __test__/graph/digraph.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { StdIn } from '@/utils'
import Bag from '@/algs4/1-3/bag'
import Digraph from '@/algs4/graph/digraph'
import DirectedDFS from '@/algs4/graph/directed-dfs'
import DepthFirstOrder from '@/algs4/graph/depth-first-order'

describe('有向图', () => {
let data: number[] | null
Expand All @@ -13,4 +16,47 @@ describe('有向图', () => {
expect(graph.countE()).toBe(15)
expect(graph.countV()).toBe(13)
})

test('有向图的可达性', () => {
const G = Digraph.createByReadIn(13, data)
const sources = new Bag<number>()
sources.add(9)

const reachable = new DirectedDFS(G, sources)
const points = []
for (let v = 0; v < G.countV(); v++) {
if (reachable.isMarked(v)) points.push(v)
}

expect(points).toStrictEqual([9, 10, 11, 12])
})

test('有向图中基于深度优先搜索的顶点排序', () => {
const G = Digraph.createByReadIn(13, data)
const dfsOrder = new DepthFirstOrder(G)

// 前序遍历
const pre = dfsOrder.getPre()
const preRes = []
while (!pre.isEmpty()) {
preRes.push(pre.dequeue())
}
expect(preRes).toStrictEqual([0, 5, 4, 1, 6, 9, 11, 12, 10, 2, 3, 7, 8])

// 后序遍历
const post = dfsOrder.getPost()
const postRes = []
while (!post.isEmpty()) {
postRes.push(post.dequeue())
}
expect(postRes).toStrictEqual([4, 5, 1, 12, 11, 10, 9, 6, 0, 3, 2, 7, 8])

// 逆后序遍历
const reversePost = dfsOrder.getReversePost()
const reversePostRes = []
while (!reversePost.isEmpty()) {
reversePostRes.push(reversePost.pop())
}
expect(reversePostRes).toStrictEqual([8, 7, 2, 3, 0, 6, 9, 10, 11, 12, 1, 5, 4])
})
})
2 changes: 1 addition & 1 deletion __test__/graph/graph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('无向图', () => {
expect(res).toStrictEqual([
[0, 1, 2, 3, 4, 5, 6],
[7, 8],
[9, 10, 11, 12],
[9, 10, 11, 12]
])
})

Expand Down
30 changes: 0 additions & 30 deletions src/algs4/graph/depth-first-order.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { StdIn } from '@/utils'
import { __DEBUG__ } from '@/constants'
import Queue from '../1-3/node-queue'
import Stack from '../1-3/node-stack'
import Digraph from './digraph'
Expand Down Expand Up @@ -47,31 +45,3 @@ export default class DepthFirshOrder {
return this.reversePost
}
}

async function main() {
const stream = await StdIn.readFile('tinyDG.txt')
const data = stream.reduce((prev, line) => [...prev, ...line.split(' ')], []).map((val: string) => +val)

const G = Digraph.createByReadIn(13, data)
const dfsOrder = new DepthFirshOrder(G)

const pre = dfsOrder.getPre()
while (!pre.isEmpty()) {
console.log(pre.dequeue())
}
console.log('-----pre queue------')

const post = dfsOrder.getPost()
while (!post.isEmpty()) {
console.log(post.dequeue())
}
console.log('-----post queue------')

const reversePost = dfsOrder.getReversePost()
while (!reversePost.isEmpty()) {
console.log(reversePost.pop())
}
console.log('-----reversePost stack------')
}

__DEBUG__ && main()
2 changes: 0 additions & 2 deletions src/algs4/graph/digraph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { StdIn } from '@/utils'
import { __DEBUG__ } from '@/constants'
import Bag from '@/algs4/1-3/bag'

/**
Expand Down
23 changes: 0 additions & 23 deletions src/algs4/graph/directed-dfs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { StdIn } from '@/utils'
import Bag from '../1-3/bag'
import Digraph from './digraph'

Expand Down Expand Up @@ -35,25 +34,3 @@ export default class DirectedDFS {
return this.marked[v]
}
}

async function main() {
const stream = await StdIn.readFile('tinyDG.txt')
const data = stream.reduce((prev, line) => [...prev, ...line.split(' ')], []).map((val: string) => +val)

const G = Digraph.createByReadIn(13, data)
const sources = new Bag<number>()

const args = [1, 2, 6]
for (let i = 0; i < args.length; i++) {
sources.add(args[i])
}

const reachable = new DirectedDFS(G, sources)
const points = []
for (let v = 0; v < G.countV(); v++) {
if (reachable.isMarked(v)) points.push(v)
}
console.log(points.join(' '))
}

main()
1 change: 0 additions & 1 deletion src/algs4/graph/symbol-graph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { StdIn } from '@/utils'
import ST from '@/algs4/search/binary-search-st'
import Graph from './graph'

Expand Down
2 changes: 1 addition & 1 deletion src/utils/std-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class StdIn {
const fileStream = fs.createReadStream(filePath)
const rl = readline.createInterface({
input: fromFile ? fileStream : process.stdin,
crlfDelay: Infinity,
crlfDelay: Infinity
})
readlineStack.length = 0
for await (const line of rl) {
Expand Down

0 comments on commit c156929

Please sign in to comment.