예제 #1
0
// partitionReverse the graph down to a subgraph starting from the given root
func partitionReverse(g osgraph.Graph, root graph.Node, buildInputEdgeKinds []string) osgraph.Graph {
	// Filter out all but BuildConfig and ImageStreamTag nodes
	nodeFn := osgraph.NodesOfKind(buildgraph.BuildConfigNodeKind, imagegraph.ImageStreamTagNodeKind)
	// Filter out all but BuildInputImage and BuildOutput edges
	edgeKinds := []string{}
	edgeKinds = append(edgeKinds, buildInputEdgeKinds...)
	edgeKinds = append(edgeKinds, buildedges.BuildOutputEdgeKind)
	edgeFn := osgraph.EdgesOfKind(edgeKinds...)
	sub := g.Subgraph(nodeFn, edgeFn)

	// Filter out inbound edges to the IST of interest
	edgeFn = osgraph.RemoveOutboundEdges([]graph.Node{root})
	sub = sub.Subgraph(nodeFn, edgeFn)

	// Check all paths leading from the root node, collect any
	// node found in them, and create the desired subgraph
	desired := []graph.Node{root}
	paths := path.DijkstraAllPaths(sub)
	for _, node := range sub.Nodes() {
		if node == root {
			continue
		}
		path, _, _ := paths.Between(node, root)
		if len(path) != 0 {
			desired = append(desired, node)
		}
	}
	return sub.SubgraphWithNodes(desired, osgraph.ExistingDirectEdge)
}
예제 #2
0
파일: a_star_test.go 프로젝트: baijum/graph
func TestExhaustiveAStar(t *testing.T) {
	g := concrete.NewGraph()
	nodes := []locatedNode{
		{id: 1, x: 0, y: 6},
		{id: 2, x: 1, y: 0},
		{id: 3, x: 8, y: 7},
		{id: 4, x: 16, y: 0},
		{id: 5, x: 17, y: 6},
		{id: 6, x: 9, y: 8},
	}
	for _, n := range nodes {
		g.AddNode(n)
	}

	edges := []weightedEdge{
		{from: g.Node(1), to: g.Node(2), cost: 7},
		{from: g.Node(1), to: g.Node(3), cost: 9},
		{from: g.Node(1), to: g.Node(6), cost: 14},
		{from: g.Node(2), to: g.Node(3), cost: 10},
		{from: g.Node(2), to: g.Node(4), cost: 15},
		{from: g.Node(3), to: g.Node(4), cost: 11},
		{from: g.Node(3), to: g.Node(6), cost: 2},
		{from: g.Node(4), to: g.Node(5), cost: 7},
		{from: g.Node(5), to: g.Node(6), cost: 9},
	}
	for _, e := range edges {
		g.SetEdge(e, e.cost)
	}

	heuristic := func(u, v graph.Node) float64 {
		lu := u.(locatedNode)
		lv := v.(locatedNode)
		return math.Hypot(lu.x-lv.x, lu.y-lv.y)
	}

	if ok, edge, goal := isMonotonic(g, heuristic); !ok {
		t.Fatalf("non-monotonic heuristic at edge:%v for goal:%v", edge, goal)
	}

	ps := path.DijkstraAllPaths(g)
	for _, start := range g.Nodes() {
		for _, goal := range g.Nodes() {
			pt, _ := path.AStar(start, goal, g, heuristic)
			gotPath, gotWeight := pt.To(goal)
			wantPath, wantWeight, _ := ps.Between(start, goal)
			if gotWeight != wantWeight {
				t.Errorf("unexpected path weight from %v to %v result: got:%s want:%s",
					start, goal, gotWeight, wantWeight)
			}
			if !reflect.DeepEqual(gotPath, wantPath) {
				t.Errorf("unexpected path from %v to %v result:\ngot: %v\nwant:%v",
					start, goal, gotPath, wantPath)
			}
		}
	}
}
예제 #3
0
func TestDijkstraAllPaths(t *testing.T) {
	for _, test := range shortestPathTests {
		g := test.g()
		for _, e := range test.edges {
			g.SetEdge(e, e.Cost)
		}

		var (
			pt path.AllShortest

			panicked bool
		)
		func() {
			defer func() {
				panicked = recover() != nil
			}()
			pt = path.DijkstraAllPaths(g.(graph.Graph))
		}()
		if panicked || test.negative {
			if !test.negative {
				t.Errorf("%q: unexpected panic", test.name)
			}
			if !panicked {
				t.Errorf("%q: expected panic for negative edge weight", test.name)
			}
			continue
		}

		// Check all random paths returned are OK.
		for i := 0; i < 10; i++ {
			p, weight, unique := pt.Between(test.query.From(), test.query.To())
			if weight != test.weight {
				t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
					test.name, weight, test.weight)
			}
			if weight := pt.Weight(test.query.From(), test.query.To()); weight != test.weight {
				t.Errorf("%q: unexpected weight from Weight: got:%f want:%f",
					test.name, weight, test.weight)
			}
			if unique != test.unique {
				t.Errorf("%q: unexpected number of paths: got: unique=%t want: unique=%t",
					test.name, unique, test.unique)
			}

			var got []int
			for _, n := range p {
				got = append(got, n.ID())
			}
			ok := len(got) == 0 && len(test.want) == 0
			for _, sp := range test.want {
				if reflect.DeepEqual(got, sp) {
					ok = true
					break
				}
			}
			if !ok {
				t.Errorf("%q: unexpected shortest path:\ngot: %v\nwant from:%v",
					test.name, p, test.want)
			}
		}

		np, weight, unique := pt.Between(test.none.From(), test.none.To())
		if np != nil || !math.IsInf(weight, 1) || unique != false {
			t.Errorf("%q: unexpected path:\ngot: path=%v weight=%f unique=%t\nwant:path=<nil> weight=+Inf unique=false",
				test.name, np, weight, unique)
		}

		paths, weight := pt.AllBetween(test.query.From(), test.query.To())
		if weight != test.weight {
			t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
				test.name, weight, test.weight)
		}

		var got [][]int
		if len(paths) != 0 {
			got = make([][]int, len(paths))
		}
		for i, p := range paths {
			for _, v := range p {
				got[i] = append(got[i], v.ID())
			}
		}
		sort.Sort(internal.BySliceValues(got))
		if !reflect.DeepEqual(got, test.want) {
			t.Errorf("testing %q: unexpected shortest paths:\ngot: %v\nwant:%v",
				test.name, got, test.want)
		}

		nps, weight := pt.AllBetween(test.none.From(), test.none.To())
		if nps != nil || !math.IsInf(weight, 1) {
			t.Errorf("%q: unexpected path:\ngot: paths=%v weight=%f\nwant:path=<nil> weight=+Inf",
				test.name, nps, weight)
		}
	}
}
예제 #4
0
파일: client.go 프로젝트: Grissess/sdnd16
func main() {

	var ipAddress, recordName, filename, input string
	var db database.RoutingDatabase
	var DBerr error

	color.Set(color.FgHiGreen, color.Bold, color.BlinkSlow)
	fmt.Println("\nWelcome to the NRA System, written in golang\n")
	color.Unset()

	fmt.Println("Enter IP address and port of the database server you'd like to use")
	fmt.Println("If none is entered, the default server will be used (testing only)\n")
	color.Unset()

	color.Set(color.FgWhite)
	fmt.Print("IP address and port >> ")
	color.Unset()

	fmt.Scanln(&ipAddress)

	if ipAddress == "" {
		color.Set(color.FgHiBlue)
		fmt.Println("No ip address selected, using default database")
		color.Unset()
		ipAddress = "128.153.144.171:6379"
	}

	color.Set(color.FgWhite)
	fmt.Print("Enter the name of the database record you wish to use >> ")
	color.Unset()
	fmt.Scanln(&recordName)

	exists, DBerr := database.TopologyExists(recordName, "tcp", ipAddress)

	if DBerr != nil {
		panic(DBerr)
	}

	if exists {
		color.Set(color.FgHiBlue)
		fmt.Println("This record exists!\n")
		color.Unset()

		db, DBerr = database.ConnectToDatabase(recordName, "tcp", ipAddress)

		topo, TPerr := db.GetTopology()

		//fmt.Printf("CLI: gb.GetTopo[%v]: %v\n", TPerr, topo)

		if DBerr != nil {
			panic(DBerr)
		}
		queryPaths(db)

	} else {
		color.Set(color.FgHiBlue)
		fmt.Println("This record does not exist.  Please check your spelling")
		color.Set(color.FgWhite)
		fmt.Print("Would you like to create a record by this name? [Y/N] >> ")
		color.Unset()
		fmt.Scanln(&input)

		if input == "Y" || input == "y" {
			color.Set(color.FgWhite)
			fmt.Print("Give me the name of a topology file >> ")
			color.Unset()

			fmt.Scanln(&filename)
			g, labels, Uerror := utils.ReadFileToGraph(filename)

			if Uerror != nil {
				panic(Uerror)
			}

			revLabels := utils.GetRevLabels(labels)
			paths := path.DijkstraAllPaths(g)
			pathMap := algorithms.ConvertAllPaths(g, paths)

			realPathMap := make(map[int]map[int]string)

			for k := range pathMap {
				smallMap := pathMap[k]
				newMap := make(map[int]string)
				for k2 := range smallMap {
					stringForPath := smallMap[k2].PathString(labels)
					newMap[k2] = stringForPath
				}
				realPathMap[k] = newMap
			}

			//fmt.Printf("CLI: pathMap: %v\n", pathMap)
			//fmt.Printf("CLI: realPathMap: %v\n", realPathMap)

			db, DBerr = database.NewRoutingDatabase(recordName, "tcp", ipAddress, revLabels, realPathMap, utils.GetNeighborMap(g))

			topo, TPerr := db.GetTopology()

			//fmt.Printf("CLI: db.GetTopo[%v]: %v\n", TPerr, topo)

			if DBerr != nil {
				panic(DBerr)
			}

			color.Set(color.FgHiGreen, color.Bold)
			fmt.Println("Database created correctly!")
			color.Unset()

			queryPaths(db)

		} else {
			color.Set(color.FgHiGreen, color.Bold)
			fmt.Println("Thank you for using golang NRA!")
			color.Unset()
		}
	}
}