如何檢測如果一個圖具有周期或不從該部分的代碼,其示出了檢測週期深度優先搜索的曲線圖是在鄰接矩陣從格拉夫
// ------------------------------------------------------------
public void dfs() // depth-first search
{ // begin at vertex 0
int k = 0;
vertexList[0].wasVisited = true; // mark it
displayVertex(0); // display it
theStack.push(0); // push it
while (!theStack.isEmpty()) // until stack empty,
{
// get an unvisited vertex adjacent to stack top
int v = getAdjUnvisitedVertex(theStack.peek());
int x = nAdjVisitedVertex(v);
if (v == -1) // if no such vertex,
theStack.pop();
else // if it exists,
{
vertexList[v].wasVisited = true; // mark it
displayVertex(v); // display it
if (x == 2)
k++;
theStack.push(v); // push it
}
} // end while
// stack is empty, so we’re done
for (int j = 0; j < nVerts; j++)
// reset flags
vertexList[j].wasVisited = false;
if(k != 0)
System.out.println("not a cycle");
else
System.out.println("cycle");
} // end dfs
你似乎在努力提出好問題。我們是一個非常有幫助的社區,但是如果您提出一個深思熟慮的問題,表明您已經付出了一些努力,那麼您會發現我們更有幫助。請閱讀該網站的FAQS。 – coder 2013-04-24 17:08:00
這是一個有向還是無向的圖? – 2013-04-24 17:08:11
@workInAFishBowl對不起,我在網上搜索,但我今天需要這個!我只需要一個想法來實現它自己而不是整個代碼。 – mpluse 2013-04-24 17:11:14