-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrooted.go
More file actions
52 lines (39 loc) · 1 KB
/
rooted.go
File metadata and controls
52 lines (39 loc) · 1 KB
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
/* Package */
package graphosalgorithms
/* Imports */
import (
"github.com/julinox/go_data_structures/graphos"
)
/* Glocals */
/* Types */
/* Interface */
/* Functions */
func RootTree(graph graphos.Grapho, node int) (graphos.Grapho) {
var visited map[int]bool
if (graph == nil) {
return nil
}
rootGraph := graphos.InitGraphList()
rootGraph.Flags |= graphos.GRAPH_DIRECTED
visited = make(map[int]bool)
rootTree(graph, rootGraph, node, visited)
return rootGraph
}
func rootTree(graph graphos.Grapho, rootGraph graphos.Grapho, node int, visited map[int]bool) {
/*
Given a node make it root (careful of which you pick)
- Create a new tree (being build at 'DFS' time)
- Do not modify original tree
*/
if (graph == nil) {
return
}
neighbours := graph.VertexNeighbours(node)
visited[node] = true
for _, v := range *neighbours {
if !(visited[v]) {
rootGraph.EdgeAdd(node, v, graph.EdgeWeight(node, v))
rootTree(graph, rootGraph, v, visited)
}
}
}