Skip to content

Commit

Permalink
feat: 符号图
Browse files Browse the repository at this point in the history
  • Loading branch information
originalix committed Mar 18, 2021
1 parent 5599a49 commit c6aa955
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
7 changes: 5 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ root = true
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,ts}]
[*.{js,ts,json}]
charset = utf-8
insert_final_newline = true

[*.{txt}]
insert_final_newline = false
52 changes: 52 additions & 0 deletions src/algs4/graph/symbol-graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { StdIn } from '@/utils'
import ST from '@/algs4/search/binary-search-st'
import Graph from './graph'

export default class SymbolGraph {
private st: ST<string, number>
private keys: string[]
private G: Graph

constructor(stream: string[], sp: string) {
this.st = new ST()
for (const line of stream) {
const a = line.split(sp)
for (let i = 0; i < a.length; i++) {
if (!this.st.contains(a[i])) {
this.st.put(a[i], this.st.size())
}
}
}
this.keys = []
for (const name of this.st.getKeys()) {
this.keys[this.st.get(name)] = name
}
this.G = new Graph(this.st.size())
for (const line of stream) {
const a = line.split(sp)
const v = this.st.get(a[0])
for (let i = 1; i < a.length; i++) {
this.G.addEdge(v, this.st.get(a[i]))
}
}
}

contains(s: string): boolean { return this.st.contains(s) }
index(s: string): number { return this.st.get(s) }
name(v: number): string { return this.keys[v] }
getG(): Graph { return this.G }
}

async function main() {
const res = await StdIn.readFile('routes.txt')
const sg = new SymbolGraph(res, ' ')
const G = sg.getG()
const routeName = 'ORD'
const adj = G.getAdj(sg.index(routeName))
while (adj.hasNext()) {
const w = adj.next()
console.log(sg.name(w))
}
}

main()
8 changes: 7 additions & 1 deletion src/algs4/search/binary-search-st.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { __DEBUG__ } from '@/constants'
/**
* 二分查找(基于有序数组)
*/
class BinarySearchST<Key, Value> {
export default class BinarySearchST<Key, Value> {
private keys: Key[]
private vals: Value[]
private N: number
Expand All @@ -18,6 +18,12 @@ class BinarySearchST<Key, Value> {

isEmpty(): boolean { return this.N === 0 }

contains(key: Key) {
return this.get(key) != null
}

getKeys() { return this.keys }

get(key: Key): Value | null {
if (this.isEmpty()) return null
const i = this.rank(key, 0, this.N - 1)
Expand Down
18 changes: 18 additions & 0 deletions src/mock/routes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
JFK MCO
ORD DEN
ORD HOU
DFW PHX
JFK ATL
ORD DFW
ORD PHX
ATL HOU
DEN PHX
PHX LAX
JFK ORD
DEN LAS
DFW HOU
ORD ATL
LAS LAX
ATL MCO
HOU MCO
LAS PHX
5 changes: 3 additions & 2 deletions src/utils/std-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export default class StdIn {
}
}

static async readFile() {
await this.processLine('string', null)
static async readFile(fileName: string = null) {
await this.processLine('string', fileName || null)
return readlineStack
}

static readString() {
Expand Down

0 comments on commit c6aa955

Please sign in to comment.