您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“c++怎么實(shí)現(xiàn)拓?fù)渑判颉钡挠嘘P(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
package com.lifeibigdata.algorithms.google; /** * Created by lifei on 16/5/23. */ import java.util.ArrayList; import java.util.List; /** * 此處的拓?fù)渑判蚴峭ㄟ^DFS的f[]降序排列。 * 另一種實(shí)現(xiàn)方法是不斷拿走入度為0的點(diǎn) * @author xiazdong * */ public class TopologicalSort_Algorithm { private static final int WHITE = 0; private static final int GRAY = 1; private static final int BLACK = 2; private int color[]; private int size; private int f[]; private int time; private Adjacent_List G; //鄰接表 private List<String> resultList; //存儲(chǔ)拓?fù)渑判虻闹档男蛄? public TopologicalSort_Algorithm(Adjacent_List G){ this.G = G; size = G.getSize(); color = new int[size]; f = new int[size]; time = 0; resultList = new ArrayList<String>(); for(int i=0;i<color.length;i++) color[i] = WHITE; } public List<String> getResultList() { return resultList; } public String[] TopologicalSort(){ DFS(); return resultList.toArray(new String[0]); } public void DFS(){ for(int i=0;i<size;i++){ if(color[i]==WHITE){ DFS_VISIT(i); } } } private void DFS_VISIT(int i) { color[i] = GRAY; time++; for(int j=0;j<G.getListByVertexIndex(i).size();j++){ String value = G.getListByVertexIndex(i).get(j); int index = G.getVertexIndex(value); if(color[index]==WHITE){ DFS_VISIT(index); } } time++; f[i] = time; resultList.add(0, G.getVertexValue(i)); //將f[i]值加入到隊(duì)列的頭部 color[i] = BLACK; } public static void main(String[] args) throws Exception { Adjacent_List adjacent_list = GraphFactory.getAdjacentListInstance("/Users/lifei/myproject/algorithms/input/topo_input.txt"); TopologicalSort_Algorithm alg = new TopologicalSort_Algorithm(adjacent_list); String[]result = alg.TopologicalSort(); for(String e:result) System.out.print(e+" "); } /** * 3 3 1 2 2 3 1 3 */ }
“c++怎么實(shí)現(xiàn)拓?fù)渑判颉钡膬?nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。