Skip to content

Commit f7c46cb

Browse files
committed
feat: add dijkstra algorithm
1 parent 5bd4e49 commit f7c46cb

File tree

10 files changed

+258
-0
lines changed

10 files changed

+258
-0
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.linting.enabled": true
3+
}

Search/Dijkstra.png

223 KB
Loading

Search/Dijkstra/main.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package BFS
2+
3+
// algorithm of graoph
4+
// BSF
5+
6+
var graph = map[string][]string{
7+
"A": []string{"B", "C"},
8+
"B": []string{"A", "C", "D"},
9+
"C": []string{"A", "B", "D", "E"},
10+
"D": []string{"B", "C", "E", "F"},
11+
"E": []string{"C", "D"},
12+
"F": []string{"D"},
13+
}
14+
15+
func BSF(graph map[string][]string, s string) []string {
16+
res := []string{}
17+
queue := []string{}
18+
queue = append([]string{s}, queue...) // 队列压入第一个
19+
seen := make(map[string]string) // 可去重的集合映射关系
20+
seen[s] = s
21+
for len(queue) > 0 {
22+
vertex := queue[len(queue)-1] // 队列推出最后一个, 为什么在可选择下一个点的队列中推出最后一个呢, 这就是广度优先了
23+
queue = queue[:len(queue)-1]
24+
nodes := graph[vertex]
25+
for i := range nodes {
26+
node := nodes[i]
27+
_, ok := seen[node]
28+
if !ok {
29+
queue = append([]string{node}, queue...) // 插入第一个
30+
seen[node] = node
31+
}
32+
}
33+
res = append(res, vertex)
34+
}
35+
return res
36+
}

Search/Dijkstra/main.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// # algorithm of graoph
2+
// # BSF
3+
4+
graph = {
5+
"A": ["B", "C"],
6+
"B": ["A", "C", "D"],
7+
"C": ["A", "B", "D", "E"],
8+
"D": ["B", "C", "E", "F"],
9+
"E": ["C", "D"],
10+
"F": ["D"],
11+
}
12+
13+
function BSF(graph, s) {
14+
// queue 队列 [a, b, c, d] 移入 [new, a, b, c] , 移除 pop
15+
queue = []
16+
queue.unshift(s)
17+
seen = new Set()
18+
seen.add(s) // 用于记录曾经访问过的节点, 不再访问
19+
while (queue.length > 0) {
20+
vertex = queue.pop()
21+
nodes = graph[vertex]
22+
for (let w of nodes) {
23+
if (!seen.has(w)) {
24+
queue.unshift(w)
25+
seen.add(w)
26+
}
27+
}
28+
console.log(vertex)
29+
}
30+
}
31+
32+
33+
// A->B->C->D->E->F
34+
BSF(graph, "A")

Search/Dijkstra/main.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# algorithm of graoph
2+
# BSF
3+
import heapq
4+
import math
5+
graph = {
6+
"A": {"B": 5, "C": 1},
7+
"B": {"A": 5, "C": 2, "D": 1},
8+
"C": {"A": 1, "B": 2, "D": 4, "E": 8},
9+
"D": {"B": 1, "C": 4, "E": 3, "F": 6},
10+
"E": {"C": 8, "D": 3},
11+
"F": {"D": 8},
12+
}
13+
14+
15+
def init_distance(graph, s):
16+
distance = {s: 0}
17+
for vertex in graph:
18+
if vertex != s:
19+
distance[vertex] = math.inf
20+
return distance
21+
22+
23+
def dijkstra(graph, s):
24+
pqueue = []
25+
heapq.heappush(pqueue, {0, s})
26+
seen = set()
27+
parent = {s: None}
28+
distance = init_distance(graph, s)
29+
30+
while (len(pqueue) > 0):
31+
pair = heapq.heappop(pqueue)
32+
dist = pair[0]
33+
vertex = pair[1]
34+
seen.add(vertex)
35+
36+
nodes = graph[vertex].keys()
37+
for w in nodes:
38+
if w not in seen:
39+
if dist + graph[vertex][w] < distance[w]:
40+
heapq.heappush(pqueue, (dist+graph[vertex][w], w))
41+
# pqueue.append(w)
42+
parent[w] = vertex
43+
distance[w] = dist + graph[vertex][w]
44+
# seen.add(w)
45+
print(vertex)
46+
return parent, distance
47+
48+
49+
parent, distance = dijkstra(graph, "A") # A->B->C->D->E->F
50+
51+
print(parent)
52+
print(distance)

Search/Dijkstra/main_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package BFS
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type para struct {
10+
one map[string][]string
11+
two string
12+
}
13+
14+
type ans struct {
15+
one []string
16+
}
17+
18+
type question struct {
19+
p para
20+
a ans
21+
}
22+
23+
func Test_OK(t *testing.T) {
24+
ast := assert.New(t)
25+
26+
qs := []question{
27+
question{
28+
p: para{
29+
one: graph,
30+
two: "A",
31+
},
32+
a: ans{
33+
one: []string{"A", "B", "C", "D", "E", "F"},
34+
},
35+
},
36+
}
37+
38+
for _, q := range qs {
39+
a, p := q.a, q.p
40+
ast.Equal(a.one, BSF(p.one, p.two), "输入:%v", p)
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# [807. Max Increase to Keep City Skyline](https://leetcode.com/problems/max-increase-to-keep-city-skyline/)
2+
3+
## 2019/06/13
4+
5+
### 题目 💗[easy]
6+
7+
In a 2 dimensional array grid, each value `grid[i][j]` represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be different for different buildings). Height 0 is considered to be a building as well.
8+
9+
At the end, the "skyline" when viewed from all four directions of the grid, i.e. top, bottom, left, and right, must be the same as the skyline of the original grid. A city's skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. See the following example.
10+
11+
What is the maximum total sum that the height of the buildings can be increased?
12+
13+
```bash
14+
Example:
15+
Input: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
16+
Output: 35
17+
18+
Explanation:
19+
The grid is:
20+
[ [3, 0, 8, 4],
21+
[2, 4, 5, 7],
22+
[9, 2, 6, 3],
23+
[0, 3, 1, 0] ]
24+
25+
The skyline viewed from top or bottom is: [9, 4, 8, 7]
26+
The skyline viewed from left or right is: [8, 7, 9, 3]
27+
28+
The grid after increasing the height of buildings without affecting skylines is:
29+
30+
gridNew = [ [8, 4, 8, 7],
31+
[7, 4, 7, 7],
32+
[9, 4, 8, 7],
33+
[3, 3, 3, 3] ]
34+
```
35+
36+
Notes:
37+
38+
1 < grid.length = grid[0].length <= 50.
39+
All heights grid[i][j] are in the range [0, 100].
40+
All buildings in grid[i][j] occupy the entire grid cell: that is, they are a 1 x 1 x grid[i][j] rectangular prism.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package bstToGst
2+
3+
import (
4+
"github.com/pengliheng/leetcode/Helper"
5+
)
6+
7+
type TreeNode = Helper.TreeNode
8+
9+
func bstToGst(root *TreeNode) *TreeNode {
10+
helper(root, 0)
11+
return root
12+
}
13+
14+
func helper(root *TreeNode, preVal int) int {
15+
if root == nil {
16+
return preVal
17+
}
18+
res := helper(root.Right, preVal)
19+
root.Val += res
20+
return helper(root.Left, root.Val)
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package bstToGst
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pengliheng/leetcode/Helper"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var tcs = []struct {
12+
N1 *TreeNode
13+
ans *TreeNode
14+
}{
15+
{
16+
Helper.Ints2TreeNode([]int{4, 1, 6, 0, 2, 5, 7, -1 << 63, -1 << 63, -1 << 63, 3, -1 << 63, -1 << 63, -1 << 63, 8}),
17+
Helper.Ints2TreeNode([]int{30, 36, 21, 36, 35, 26, 15, -1 << 63, -1 << 63, -1 << 63, 33, -1 << 63, -1 << 63, -1 << 63, 8}),
18+
},
19+
}
20+
21+
func Test_bitwiseComplement(t *testing.T) {
22+
ast := assert.New(t)
23+
for _, tc := range tcs {
24+
ast.Equal(tc.ans, bstToGst(tc.N1), "输入:%v", tc)
25+
}
26+
}

algorithm/141.LinkedListCycle/main_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ var tcs = []struct {
1515
Helper.Ints2ListWithCycle([]int{3, 2, 0, -4}, 1),
1616
true,
1717
},
18+
{
19+
Helper.Ints2ListWithCycle([]int{3, 2, 0, -4}, -1),
20+
false,
21+
},
1822
}
1923

2024
func Test_bitwiseComplement(t *testing.T) {

0 commit comments

Comments
 (0)