Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b85d400

Browse files
data_structures: graph dfs implementation
1 parent e00e985 commit b85d400

File tree

7 files changed

+133
-24
lines changed

7 files changed

+133
-24
lines changed

‎data_structures/graph/adjacency_list/operations.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package adjacency_list
22

33
func (g *Graph) Adjacent(x, y int) bool {
44
if nodeX, ok := g.adjList[x]; ok {
5-
for _, v :=range nodeX {
6-
if v==y {
5+
for _, v :=range nodeX {
6+
if v==y {
77
return true
88
}
99
}
@@ -18,7 +18,7 @@ func (g *Graph) Neighbors(x int) []int {
1818
return []int{}
1919
}
2020

21-
func (g *Graph) AddVertex(x int) {
21+
func (g *Graph) AddVertex(x int) {
2222
if _, ok := g.adjList[x]; !ok {
2323
g.adjList[x] = make([]int, 0, 4)
2424
g.values[x] = nil
@@ -32,7 +32,7 @@ func (g *Graph) RemoveVertex(x int) {
3232
}
3333
}
3434

35-
func (g *Graph) AddEdge(x, y int) {
35+
func (g *Graph) AddEdge(x, y int) {
3636
if g.Adjacent(x, y) {
3737
return
3838
}
@@ -48,7 +48,7 @@ func (g *Graph) RemoveEdge(x, y int) {
4848
// Move last element to the position of element, that would be deleted
4949
// And truncates slice (remove last element)
5050
nodeX[idx] = nodeX[len(nodeX)-1]
51-
g.adjList[x] = nodeX[0:len(nodeX)-1]
51+
g.adjList[x] = nodeX[0 : len(nodeX)-1]
5252
}
5353
}
5454
}
@@ -66,4 +66,3 @@ func (g *Graph) SetVertexValue(x int, v interface{}) (ok bool) {
6666
g.values[x] = v
6767
return true
6868
}
69-

‎data_structures/graph/adjacency_list/operations_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
var (
88
graph *Graph
99

10+
// 0 1 2 3 4 comment is here because of go fmt ...
1011
testGraphAdjMaxtrix = [][]int{
11-
// 0 1 2 3 4
1212
{0, 1, 1, 0, 0}, // 0
1313
{0, 0, 0, 0, 0}, // 1
1414
{0, 0, 0, 0, 0}, // 2
@@ -36,7 +36,6 @@ func initGraph() {
3636
}
3737
}
3838

39-
4039
func TestGraph_Adjacent(t *testing.T) {
4140
for x, _ := range testGraphAdjMaxtrix {
4241
for y, _ := range testGraphAdjMaxtrix {
@@ -49,4 +48,3 @@ func TestGraph_Adjacent(t *testing.T) {
4948
}
5049
}
5150
}
52-

‎data_structures/graph/adjacency_list/representation.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ package adjacency_list
33

44
type Graph struct {
55
adjList map[int][]int
6-
values map[int]interface{}
6+
values map[int]interface{}
77
}
88

99
func New() *Graph {
1010
return &Graph{
1111
adjList: make(map[int][]int),
12-
values: make(map[int]interface{}),
12+
values: make(map[int]interface{}),
1313
}
1414
}
15-
16-

‎data_structures/graph/adjacency_matrix/representation.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ remove_edge(G, x, y): removes the edge from the vertex x to the vertex y, if it
1414
get_vertex_value(G, x): returns the value associated with the vertex x;
1515
set_vertex_value(G, x, v): sets the value associated with the vertex x to v.
1616
*/
17-

‎data_structures/graph/algorithms/dfs.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://en.wikipedia.org/wiki/Depth-first_search
2+
package algorithms
3+
4+
import (
5+
"github.com/emikhalev/algorithm/data_structures/graph/adjacency_list"
6+
)
7+
8+
func DFS(graph *adjacency_list.Graph, startVertexIdx int,
9+
fnForwardCallback, fnBackCallback func(nodeIdx int)) {
10+
11+
visited := make(map[int]struct{})
12+
dfs(graph, visited, startVertexIdx, fnForwardCallback, fnBackCallback)
13+
}
14+
15+
func dfs(graph *adjacency_list.Graph, visited map[int]struct{},
16+
v int, fnForward, fnBack func(nodeIdx int)) {
17+
18+
fnForward(v)
19+
visited[v] = struct{}{}
20+
adjEdges := graph.Neighbors(v)
21+
for _, w := range adjEdges {
22+
if _, ok := visited[w]; !ok {
23+
dfs(graph, visited, w, fnForward, fnBack)
24+
}
25+
}
26+
27+
fnBack(v)
28+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package algorithms
2+
3+
import (
4+
"testing"
5+
6+
"github.com/emikhalev/algorithm/data_structures/graph/adjacency_list"
7+
)
8+
9+
var (
10+
graph *adjacency_list.Graph
11+
12+
/*
13+
0
14+
/ \
15+
1 --2
16+
\/ \
17+
3 - 4
18+
19+
*/
20+
// 0 1 2 3 4 comment is here because of go fmt ...
21+
testGraphAdjMaxtrix = [][]int{
22+
{0, 1, 1, 0, 0}, // 0
23+
{0, 0, 0, 0, 0}, // 1
24+
{0, 0, 1, 1, 0}, // 2
25+
{0, 0, 0, 0, 1}, // 3
26+
{0, 0, 0, 0, 0}, // 4
27+
}
28+
)
29+
30+
func TestMain(m *testing.M) {
31+
initGraph()
32+
m.Run()
33+
}
34+
35+
func initGraph() {
36+
graph = adjacency_list.New()
37+
for idx, _ := range testGraphAdjMaxtrix {
38+
graph.AddVertex(idx)
39+
}
40+
for x, _ := range testGraphAdjMaxtrix {
41+
for y, _ := range testGraphAdjMaxtrix {
42+
if testGraphAdjMaxtrix[x][y] == 1 {
43+
graph.AddEdge(x, y)
44+
}
45+
}
46+
}
47+
}
48+
49+
func TestDFS(t *testing.T) {
50+
// Expecting: 0(in)->1(in)->1(out)->2(in)->3(in)->4(in)->4(out)->3(out)->2(out)->0(out)
51+
const (
52+
DirIn = 0
53+
DirOut = 1
54+
)
55+
exp := []struct {
56+
id int
57+
direction int
58+
}{
59+
{0, DirIn},
60+
{1, DirIn},
61+
{1, DirOut},
62+
{2, DirIn},
63+
{3, DirIn},
64+
{4, DirIn},
65+
{4, DirOut},
66+
{3, DirOut},
67+
{2, DirOut},
68+
{0, DirOut},
69+
}
70+
71+
expIdx := 0
72+
checkFn := func(nodeIdx int, dir int) {
73+
if nodeIdx != exp[expIdx].id || dir != exp[expIdx].direction {
74+
t.Errorf("expIdx: %d) expecting id=%d, direction=%d, got id=%d, direction=%d",
75+
expIdx,
76+
exp[expIdx].id, exp[expIdx].direction,
77+
nodeIdx, dir)
78+
}
79+
expIdx++
80+
}
81+
DFS(graph, 0,
82+
func(nodeIdx int) {
83+
checkFn(nodeIdx, DirIn)
84+
},
85+
func(nodeIdx int) {
86+
checkFn(nodeIdx, DirOut)
87+
},
88+
)
89+
}

‎leetcode/minimum_remove_to_make_valid_parentheses-solution-1.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ func minRemoveToMakeValid(s string) string {
77
}
88

99
func into(s []byte, pos, level int) ([]byte, int) {
10-
for i:=pos; i<len(s); i++ {
10+
for i:=pos; i<len(s); i++ {
1111
c := s[i]
12-
if c=='(' {
12+
if c=='(' {
1313
s, i = into(s, i+1, level+1)
1414
continue
1515
}
16-
if c==')' {
17-
if level>0 {
16+
if c==')' {
17+
if level>0 {
1818
return s, i
1919
} else {
20-
if i+1<len(s) {
20+
if i+1<len(s) {
2121
s = append(s[:i], s[i+1:]...)
2222
} else {
2323
s = s[:i]
@@ -26,15 +26,13 @@ func into(s []byte, pos, level int) ([]byte, int) {
2626
}
2727
}
2828
}
29-
if level>0 {
30-
if pos<len(s) {
29+
if level>0 {
30+
if pos<len(s) {
3131
s = append(s[:pos-1], s[pos:]...)
3232
} else {
3333
s = s[:pos-1]
3434
}
35-
return s, len(s)-1
35+
return s, len(s)-1
3636
}
3737
return s, len(s)
3838
}
39-
40-

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /