本篇內(nèi)容介紹了“c++怎么實(shí)現(xiàn)拓?fù)渑判颉钡挠嘘P(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)是專業(yè)的甘井子網(wǎng)站建設(shè)公司,甘井子接單;提供網(wǎng)站制作、做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行甘井子網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
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)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)站標(biāo)題:c++怎么實(shí)現(xiàn)拓?fù)渑判?/a>
鏈接地址:http://jinyejixie.com/article20/gpeijo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、全網(wǎng)營(yíng)銷推廣、App設(shè)計(jì)、關(guān)鍵詞優(yōu)化、電子商務(wù)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)