diff --git a/README.md b/README.md index 1fdc5ac..757184a 100644 --- a/README.md +++ b/README.md @@ -62,16 +62,9 @@ $ pnpm bench twoSum.bench.ts └── vite.config.ts ``` -## Beginning - The Algorithms - -- [Implements various algorithms and data structures in TypeScript](https://the-algorithms.com/language/typescript) - -### Data Structures - -- [圖 (Graph)](./data-structures/graph/README.md) - -### Algorithms +## Beginning - Algorithms +- [圖 (Graph)](./algorithms/graph/README.md) - [二分搜尋 (Binary Search)](./algorithms/binary-search/README.md) ## Basic - LeetCode 75 diff --git a/data-structures/graph/Graph.ts b/algorithms/graph/Graph.ts similarity index 97% rename from data-structures/graph/Graph.ts rename to algorithms/graph/Graph.ts index 64efd5e..f31f939 100644 --- a/data-structures/graph/Graph.ts +++ b/algorithms/graph/Graph.ts @@ -1,6 +1,6 @@ class Graph { // 鄰接表 - private adjacencyList: Map; + adjacencyList: Map; constructor(initialAdjList?: { [key: string]: string[] }) { this.adjacencyList = new Map(); diff --git a/data-structures/graph/README.md b/algorithms/graph/README.md similarity index 69% rename from data-structures/graph/README.md rename to algorithms/graph/README.md index 62001a8..2022ee1 100644 --- a/data-structures/graph/README.md +++ b/algorithms/graph/README.md @@ -3,9 +3,10 @@ ## 鄰接表 - 雜湊表方式 ```ts +// Graph.ts class Graph { // 鄰接表 - private adjacencyList: Map; + adjacencyList: Map; constructor(initialAdjList?: { [key: string]: string[] }) { this.adjacencyList = new Map(); @@ -68,7 +69,9 @@ class Graph { } } } +``` +```ts const graph = new Graph(); graph.addVertex('A'); @@ -119,3 +122,38 @@ const graph = new Graph({ E: ['B', 'D'], }); ``` + +## 廣度優先搜尋 (Breadth-first Search) + +```ts +// bfs.ts +function bfs(adjacencyList: Map, startVertex: string): void { + const queue: string[] = [startVertex]; // 使用陣列作為佇列 + const visited = new Set(); // 記錄已訪問的頂點 + + visited.add(startVertex); // 標記起始頂點為已訪問 + + while (queue.length > 0) { + const current = queue.shift(); // 取出佇列的第一個元素 + if (current === undefined) continue; + + console.log(current); // 當前訪問的頂點 + + // 走訪所有相鄰的頂點 + for (const neighbor of adjacencyList.get(current) || []) { + // 如果未訪問 + if (!visited.has(neighbor)) { + visited.add(neighbor); // 標記為已訪問 + queue.push(neighbor); // 加入佇列 + } + } + } +} +``` + +```ts +// 開始廣度優先搜尋,從頂點 'A' 開始 +bfs(graph.adjacencyList, 'A'); +``` + +## 深度優先搜尋 (Depth-first Search) diff --git a/algorithms/graph/bfs.ts b/algorithms/graph/bfs.ts new file mode 100644 index 0000000..2cdc309 --- /dev/null +++ b/algorithms/graph/bfs.ts @@ -0,0 +1,22 @@ +function bfs(adjacencyList: Map, startVertex: string): void { + const queue: string[] = [startVertex]; // 使用陣列作為佇列 + const visited = new Set(); // 記錄已訪問的頂點 + + visited.add(startVertex); // 標記起始頂點為已訪問 + + while (queue.length > 0) { + const current = queue.shift(); // 取出佇列的第一個元素 + if (current === undefined) continue; + + console.log(current); // 當前訪問的頂點 + + // 走訪所有相鄰的頂點 + for (const neighbor of adjacencyList.get(current) || []) { + // 如果未訪問 + if (!visited.has(neighbor)) { + visited.add(neighbor); // 標記為已訪問 + queue.push(neighbor); // 加入佇列 + } + } + } +}