Skip to content

Commit

Permalink
218th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 11, 2024
1 parent cbb322b commit a14d27f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Graph {
// 鄰接表
private adjacencyList: Map<string, string[]>;
adjacencyList: Map<string, string[]>;

constructor(initialAdjList?: { [key: string]: string[] }) {
this.adjacencyList = new Map();
Expand Down
40 changes: 39 additions & 1 deletion data-structures/graph/README.md → algorithms/graph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
## 鄰接表 - 雜湊表方式

```ts
// Graph.ts
class Graph {
// 鄰接表
private adjacencyList: Map<string, string[]>;
adjacencyList: Map<string, string[]>;

constructor(initialAdjList?: { [key: string]: string[] }) {
this.adjacencyList = new Map();
Expand Down Expand Up @@ -68,7 +69,9 @@ class Graph {
}
}
}
```

```ts
const graph = new Graph();

graph.addVertex('A');
Expand Down Expand Up @@ -119,3 +122,38 @@ const graph = new Graph({
E: ['B', 'D'],
});
```

## 廣度優先搜尋 (Breadth-first Search)

```ts
// bfs.ts
function bfs(adjacencyList: Map<string, string[]>, startVertex: string): void {
const queue: string[] = [startVertex]; // 使用陣列作為佇列
const visited = new Set<string>(); // 記錄已訪問的頂點

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)
22 changes: 22 additions & 0 deletions algorithms/graph/bfs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function bfs(adjacencyList: Map<string, string[]>, startVertex: string): void {
const queue: string[] = [startVertex]; // 使用陣列作為佇列
const visited = new Set<string>(); // 記錄已訪問的頂點

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); // 加入佇列
}
}
}
}

0 comments on commit a14d27f

Please sign in to comment.