예제 #1
0
func ExampleGraph_MinimumSpanningTree() {
	g := graph.New(graph.Undirected)
	nodes := make(map[rune]graph.Node, 0)
	nodes['a'] = g.MakeNode()
	nodes['b'] = g.MakeNode()
	nodes['c'] = g.MakeNode()
	nodes['d'] = g.MakeNode()
	nodes['e'] = g.MakeNode()
	nodes['f'] = g.MakeNode()
	nodes['g'] = g.MakeNode()
	nodes['h'] = g.MakeNode()
	nodes['i'] = g.MakeNode()
	g.MakeEdgeWeight(nodes['a'], nodes['b'], 4)
	g.MakeEdgeWeight(nodes['a'], nodes['h'], 8)
	g.MakeEdgeWeight(nodes['b'], nodes['c'], 8)
	g.MakeEdgeWeight(nodes['b'], nodes['h'], 11)
	g.MakeEdgeWeight(nodes['c'], nodes['d'], 7)
	g.MakeEdgeWeight(nodes['c'], nodes['f'], 4)
	g.MakeEdgeWeight(nodes['c'], nodes['i'], 2)
	g.MakeEdgeWeight(nodes['d'], nodes['e'], 9)
	g.MakeEdgeWeight(nodes['d'], nodes['f'], 14)
	g.MakeEdgeWeight(nodes['e'], nodes['f'], 10)
	g.MakeEdgeWeight(nodes['f'], nodes['g'], 2)
	g.MakeEdgeWeight(nodes['g'], nodes['h'], 1)
	g.MakeEdgeWeight(nodes['g'], nodes['i'], 6)
	g.MakeEdgeWeight(nodes['h'], nodes['i'], 7)
	mst := g.MinimumSpanningTree()
	weightSum := 0
	for i := range mst {
		weightSum += mst[i].Weight
	}
	fmt.Println(weightSum)
	// Output: 37
}
예제 #2
0
func ExampleGraph_TopologicalSort() {
	g := graph.New(graph.Directed)

	clothes := make(map[string]graph.Node, 0)
	// Make a mapping from strings to a node
	clothes["shirt"] = g.MakeNode()
	clothes["tie"] = g.MakeNode()
	clothes["jacket"] = g.MakeNode()
	clothes["belt"] = g.MakeNode()
	clothes["watch"] = g.MakeNode()
	clothes["undershorts"] = g.MakeNode()
	clothes["pants"] = g.MakeNode()
	clothes["shoes"] = g.MakeNode()
	clothes["socks"] = g.MakeNode()
	// Make references back to the string values
	for key, node := range clothes {
		*node.Value = key
	}
	// Connect the elements
	g.MakeEdge(clothes["shirt"], clothes["tie"])
	g.MakeEdge(clothes["tie"], clothes["jacket"])
	g.MakeEdge(clothes["shirt"], clothes["belt"])
	g.MakeEdge(clothes["belt"], clothes["jacket"])
	g.MakeEdge(clothes["undershorts"], clothes["pants"])
	g.MakeEdge(clothes["undershorts"], clothes["shoes"])
	g.MakeEdge(clothes["pants"], clothes["belt"])
	g.MakeEdge(clothes["pants"], clothes["shoes"])
	g.MakeEdge(clothes["socks"], clothes["shoes"])
	sorted := g.TopologicalSort()
	for i := range sorted {
		fmt.Println(*sorted[i].Value)
	}
	// Output:
	// socks
	// undershorts
	// pants
	// shoes
	// watch
	// shirt
	// belt
	// tie
	// jacket
}
예제 #3
0
// Sort the node targets based on dependencies (using a graph-sort)
func (targets *Targets) Sort() bool {
	logger := targets.log.MakeChild("sort")
	logger.Debug(log.VERBOSITY_DEBUG, "Starting target sort:", targets, targets.TargetOrder())

	g := graph.New(graph.Directed)
	graphNodes := make(map[string]graph.Node, 0)
	targetMap := map[string]*Target{}

	// use targets to create graph nodes
	for _, name := range targets.TargetOrder() {
		targetMap[name], _ = targets.Target(name)
		graphNodes[name] = g.MakeNode()
		*graphNodes[name].Value = name
		logger.Debug(log.VERBOSITY_DEBUG_STAAAP, "ADDING TARGET:"+name, nil)
	}
	// use dependencies to set graph edges (can't be done in the above loop)
	for _, outerName := range targets.TargetOrder() {
		for _, innerName := range targets.TargetOrder() {
			outerTarget, _ := targets.Target(outerName)
			outerNode, _ := outerTarget.Node()

			if outerNode.DependsOn(innerName) {
				g.MakeEdge(graphNodes[innerName], graphNodes[outerName])
				logger.Debug(log.VERBOSITY_DEBUG_STAAAP, "ADDING EDGE:"+innerName+"<-"+outerName, nil)
			}
		}
	}

	sorted := []string{}
	for _, graphNode := range g.TopologicalSort() {
		value := *graphNode.Value
		if name, ok := value.(string); ok {
			sorted = append(sorted, name)
		}
	}

	logger.Debug(log.VERBOSITY_DEBUG_STAAAP, "sort SORTED:", sorted)
	targets.targetOrder = sorted
	return true
}
예제 #4
0
type Jumproute struct {
	To_system   int `bson:"to_system"`
	From_system int `bson:"from_system"`
}

var jumproutes []Jumproute

type System struct {
	Id            int     `bson:"_id"`
	SecurityLevel float32 `bson:"securityLevel"`
}

var systems []System

var g = graph.New(graph.Undirected)

func handler(w http.ResponseWriter, r *http.Request) {
	var request = r.URL.Query()
	fmt.Println(request)
}

func getJumproutes() {
	session, err := mgo.Dial("127.0.0.1:27017")
	if err != nil {
		log.Fatal(err)
	}
	defer session.Close()
	session.SetMode(mgo.Monotonic, true)
	jr := session.DB("evecalc").C("jumproutes")
	sy := session.DB("evecalc").C("systems")