-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
146 lines (124 loc) · 2.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println("GoLang, Graph DFS and BFS implementation")
fmt.Println("DFS : Depth First Search")
fmt.Println("BFS : Breadth First Search")
g := NewGraph()
g.AddVertex("ajinkya")
g.AddVertex("francesc")
g.AddVertex("manish")
g.AddVertex("albert")
g.AddEdge("albert", "ajinkya")
g.AddEdge("ajinkya", "albert")
g.AddEdge("francesc", "ajinkya")
g.AddEdge("francesc", "manish")
g.AddEdge("manish", "francesc")
g.AddEdge("manish", "albert")
g.DFS("francesc")
g.CreatePath("francesc", "albert")
}
func NewGraph() Graph {
return Graph{
adjacency: make(map[string][]string),
}
}
type Graph struct {
adjacency map[string][]string
}
func (g *Graph) AddVertex(vertex string) bool {
if _, ok := g.adjacency[vertex]; ok {
fmt.Printf("vertex %v already exists\n", vertex)
return false
}
g.adjacency[vertex] = []string{}
return true
}
func (g *Graph) AddEdge(vertex, node string) bool {
if _, ok := g.adjacency[vertex]; !ok {
fmt.Printf("vertex %v does not exists\n", vertex)
return false
}
if ok := contains(g.adjacency[vertex], node); ok {
fmt.Printf("node %v already exists\n", node)
return false
}
g.adjacency[vertex] = append(g.adjacency[vertex], node)
return true
}
func (g Graph) BFS(startingNode string) {
visited := g.createVisited()
var q []string
visited[startingNode] = true
q = append(q, startingNode)
for len(q) > 0 {
var current string
current, q = q[0], q[1:]
fmt.Println("BFS", current)
for _, node := range g.adjacency[current] {
if !visited[node] {
q = append(q, node)
visited[node] = true
}
}
}
}
func (g Graph) DFS(startingNode string) {
visited := g.createVisited()
g.dfsRecursive(startingNode, visited)
}
func (g Graph) dfsRecursive(startingNode string, visited map[string]bool) {
visited[startingNode] = true
fmt.Println("DFS", startingNode)
for _, node := range g.adjacency[startingNode] {
if !visited[node] {
g.dfsRecursive(node, visited)
}
}
}
func (g Graph) CreatePath(firstNode, secondNode string) bool {
visited := g.createVisited()
var (
path []string
q []string
)
q = append(q, firstNode)
visited[firstNode] = true
for len(q) > 0 {
var currentNode string
currentNode, q = q[0], q[1:]
path = append(path, currentNode)
edges := g.adjacency[currentNode]
if contains(edges, secondNode) {
path = append(path, secondNode)
fmt.Println(strings.Join(path, "->"))
return true
}
for _, node := range g.adjacency[currentNode] {
if !visited[node] {
visited[node] = true
q = append(q, node)
}
}
}
fmt.Println("no link found")
return false
}
func (g Graph) createVisited() map[string]bool {
visited := make(map[string]bool, len(g.adjacency))
for key := range g.adjacency {
visited[key] = false
}
return visited
}
func contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set[s] = struct{}{}
}
_, ok := set[item]
return ok
}