-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.go
More file actions
75 lines (61 loc) · 1.5 KB
/
Copy pathgraph.go
File metadata and controls
75 lines (61 loc) · 1.5 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
// Node - basic graph node with path cost
type Node struct {
PathCost int
Clause *TreeNode
Truth bool
}
// Edge - directed graph edge with weight if graph will be
// weighted
type Edge struct {
Start *Node
End *Node
Weight int
}
// Graph - the data structure that holds the nodes and their
// connections
type Graph struct {
Nodes []*Node
TruthNode *Node
Edges map[*Node][]*Node
TreeNodeMap map[*TreeNode]*Node
NodeExists map[*Node]bool
}
// CreateGraph - creates the initial graph with the truth node as
// the starting node
func CreateGraph() *Graph {
g := Graph{
Edges: make(map[*Node][]*Node),
TreeNodeMap: make(map[*TreeNode]*Node),
NodeExists: make(map[*Node]bool)}
truthNode := Node{Truth: true}
g.AddNode(&truthNode)
return &g
}
// AddNode add a node to the graph
func (g *Graph) AddNode(n *Node) {
if !g.NodeExists[n] {
if n.Truth {
g.TruthNode = n
}
n.PathCost = 0
g.Nodes = append(g.Nodes, n)
g.NodeExists[n] = true
}
}
func (g *Graph) AddEdge(from *Node, to *Node, weight int) {
to.PathCost = from.PathCost + 1
g.Edges[from] = append(g.Edges[from], to)
}
// GetTruthNode - returns the truth node in the graph
func (g *Graph) GetTruthNode() *Node {
return g.TruthNode
}
// GetConnectedNodes - get a list of all the reachable nodes (with distance 1)
// from a source node n
func (g *Graph) GetConnectedNodes(n *Node) (nodes []*Node) {
return g.Edges[n]
}
func (g *Graph) MapTreeNode(t *TreeNode) *Node {
return g.TreeNodeMap[t]
}