예제 #1
0
//Use in Crucialpath
func (a *Graph) backward() {
	q := Queue.NewGoQueueSize(a.n)
	q.MakeNull()
	// this := a.copy
	b := a.reverse()
	// b.Print()

	var inde []int
	inde = make([]int, b.n) //indegree
	vl[b.n-1] = ve[b.n-1]
	for i := 0; i < b.n; i++ {
		inde[i] = indegree(i, *b)
		if inde[i] == 0 {
			q.Enter(i)
		}
	}
	// fmt.Println(inde)

	nodecount := 0
	for q.Empty() == nil {
		// q.Print()
		// fmt.Println("")
		t, _ := q.Front()
		x := reflect.ValueOf(t)
		v := int(x.Int())

		q.Delete()
		tmp := b.headlist[v].firstedge

		for i := 0; i < b.n; i++ {
			if act[v*b.n+i] > 0 && vl[v] < vl[i]+act[v*b.n+i] {
				vl[v] = vl[i] - act[v*b.n+i]
			}
		}
		nodecount++

		tmp = b.headlist[v].firstedge
		for tmp != nil {
			inde[tmp.adjvex]--
			if inde[tmp.adjvex] == 0 {
				q.Enter(tmp.adjvex)
			}
			tmp = tmp.next
		}
	}
	if nodecount < 0 {
		panic(errors.New("There is cycle on the digraph!"))
	}
}
예제 #2
0
//Use in Crucialpath
func (a *Graph) forward() {
	q := Queue.NewGoQueueSize(a.n)
	q.MakeNull()
	// this := a.copy
	var inde []int
	inde = make([]int, a.n) //indegree
	for i := 0; i < a.n; i++ {
		inde[i] = indegree(i, *a)
		if inde[i] == 0 {
			q.Enter(i)
		}
	}
	// fmt.Println(inde)
	ve[1] = 0
	nodecount := 0
	for q.Empty() == nil {
		// q.Print()
		t, _ := q.Front()
		b := reflect.ValueOf(t)
		v := int(b.Int())

		q.Delete()

		for i := 0; i < a.n; i++ {
			if act[i*a.n+v] > 0 && ve[v] < ve[i]+act[i*a.n+v] {
				ve[v] = ve[i] + act[i*a.n+v]
			}
		}
		nodecount++

		tmp := a.headlist[v].firstedge
		for tmp != nil {
			inde[tmp.adjvex]--
			if inde[tmp.adjvex] == 0 {
				q.Enter(tmp.adjvex)
			}
			tmp = tmp.next
		}
	}
	if nodecount < 0 {
		panic(errors.New("There is cycle on the digraph!"))
	}
}
예제 #3
0
func (a *Graph) TopoOrder() {
	q := Queue.NewGoQueueSize(a.n)
	q.MakeNull()
	// this := a.copy
	var inde []int
	inde = make([]int, a.n) //indegree
	for i := 0; i < a.n; i++ {
		inde[i] = indegree(i, *a)
		if inde[i] == 0 {
			q.Enter(i)
		}
	}
	// fmt.Println(inde)

	nodecount := 0
	for q.Empty() == nil {
		// q.Print()
		// fmt.Println("")
		t, _ := q.Front()
		b := reflect.ValueOf(t)
		v := int(b.Int())

		q.Delete()
		fmt.Print(v, " ")
		nodecount++

		tmp := a.headlist[v].firstedge
		for tmp != nil {
			inde[tmp.adjvex]--
			if inde[tmp.adjvex] == 0 {
				q.Enter(tmp.adjvex)
			}
			tmp = tmp.next
		}
	}
	if nodecount < 0 {
		fmt.Println("There is cycle on the digraph!")
	} else {
		fmt.Println("")
	}

}