topological-sort

dfs:

// dfs
	public void depthFirstSearch() {
		int i;
		for (i = 0; i < this.nVertexs; i++)
			vertexSet[i].setColor(0);
		for (i = 0; i < this.nVertexs; i++)
			if (vertexSet[i].getColor() == 0)
				dfsVisit(i);
	}

	// dfsVisit
	private void dfsVisit(int index) {
		Vertex indexVertex = vertexSet[index];
		indexVertex.setColor(1);
		// begin time
		System.out.print("顶点" + indexVertex.getV() + "  ");
		for (int i = 0; i < indexVertex.getNextList().size(); i++) {
			Vertex vertexAdjIndex = indexVertex.getNextList().get(i);
			if (vertexAdjIndex.getColor() == 0)
				dfsVisit(vertexAdjIndex.getIndex());
		}
		// end time
		//insert this vertex to linked list
	}



topological_sort

0, call dfs(G) to compute finishing times f[v] for each vertex v

1, as each vertex is finished , insert it onto the front of a linked list

2, return the linked list of vertices

你可能感兴趣的:(0-Java/Android,1-Algorithms)