-
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.
- Loading branch information
1 parent
5599a49
commit c6aa955
Showing
5 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
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,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() |
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,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 |
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