问个关于算法的小问题

rottenapple 2006-01-05 09:02:52
一个面试的问题,用到数据结构,可惜都忘记了。
真变态,大家帮个忙阿。

有一个图,上面有A,B,C,D,E五个点,其中相互之间的路径连接如下(单向的)

Point1, Point2, Time
A,B,2
B,D,1
A,C,3
C,E,3
A,E,6
D,E,1
....
现在想写三个算法,分别完成下列功能:
1。列出所有A->E的可能路径
2。列出时间最短路径
3。列出时间最长路径。
我个人觉得如果问题1解决了,问题2,3也不难。
谁能给出问题1的解法呢?
...全文
234 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
dugu9527 2006-01-10
  • 打赏
  • 举报
回复
public String findLine(String A,String B,int k,String f){
String C=f+"";
for(int i=k;i<6;i++){

if(point[i][0].toString().equalsIgnoreCase(A)){
if(point[i][1].toString().equalsIgnoreCase(B)){

C=C+point[i][0]+"->"+point[i][1];
System.out.println(C);
C="";
continue;
}else{
C=C+point[i][0]+"->"+point[i][1]+"->";

String m=C;
C="";
findLine(point[i][1],"E",i+1,m);
continue;
}
}else{
continue;
}

}

return C;
}
---------------------------------------------------------------------------
以上是我的算法,不知道正确与否,可以得到从A->E的可能路径,至于时间长短加个参数就可以了吧,有疑问的话与我联系,很希望和各位探讨,MSN:dugulili@hotmail.com
zhenming_liu 2006-01-08
  • 打赏
  • 举报
回复
The 3rd problem (the longest path) in general is NP-Complete, only dynamic programming works.
f_acme 2006-01-07
  • 打赏
  • 举报
回复
void WaySearch(int v0,int end)
{
Vnode *p;
int i;
if(v0==end)
{
for(i=0;i<edgenum;i++){
printf("%d(%s)->",way[i],city[way[i]]);
}
printf("%d(%s)\n路径长度为:%d\n\n",end,city[end],sum);
getch();
}
visit[v0]=1;
p=Adjlist[v0]->next;
way[edgenum++]=v0;
while(p!=NULL)
{
if(!visit[p->NumOfVex]){
sum=sum+p->value;
WaySearch(p->NumOfVex,end);
sum=sum-p->value;
}
p=p->next;
}
visit[v0]=0;
edgenum--;
}
jp1984 2006-01-06
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#define VSIZE 7 /* change the number of vertices fix here*/

/**
A Test Program Of DFS(Depth First Search)
refer <to Introduction To Algorithm >
* Math Frog@SHFU
* jp021@hotmail.com
* 2003.5.2
* Dfs is a very important searching technique ,
In fact if we ignore the computation ability of
the computer,DFS is always useful to solve all
the searching problem.
* Now we make DFS just on graph traversing.The
problem is given one start vertice in graph,reach
all the vertice in graph.This procedure is very
similar to preorder tree traversing.
* In our algorithm there are three state for
the vertice,WHITE,GRAY,BLACK.When the vertex is
WHITE,it tells us that the vertice is not discovered
yet.And when the vertice is GRAY,it tells us that the
vertex is just discovered.If black,the vertice is
visited.These three states are very important in
graph algorithm.
* When coding,we represent the graph using adjacent
matrix,of course using adjacent link list is just
the same.Because adjacent matrix is easy to code and
more clear to understand.And the graph is direct,
because direct graph is used much more widely in
practice then undirect graph.
**/

/* global var */
int graph[VSIZE][VSIZE]; /* the graph's adjacent matrix */
int vSet[VSIZE]; /* set of the vertice */
enum state {WHITE,GRAY,BLACK}; /* the three state */
enum state vSetState[VSIZE]; /* state of every vertex */

/* function declaration */
void inputGraph(); /* get graph */
void dispGraph(); /* display graph */

void DFS(); /* the overall function to traverse graph */
void DFS_visit(int);/* to reach the vertex that can be reach from given vetex */

/** To be simple input graph means you
just input the graph's adjacent matrix.
*/
void inputGraph()
{
int i,j;
printf("Input the adjacent matrix of the graph \n");
printf("___________________________\n");
for(i = 0;i < VSIZE;i++)
for(j = 0;j < VSIZE;j++)
scanf("%d",&graph[i][j]);
for(i = 0;i < VSIZE;i++)
vSet[i] = i + 1;/* be default the vertice are 1,2...VSIZE */
}

void dispGraph()
{
int i,j;
printf("___________________________\n");
printf("The vertice of this graph are \n");
for(i = 0;i < VSIZE;i++)
printf("%d ",vSet[i]);
printf("\n___________________________\n");
printf("\nThe adjacent matrix of this graph is \n");
for(i = 0;i < VSIZE;i++){
for(j = 0;j < VSIZE;j++)
printf("%d ",graph[i][j]);
printf("\n");
}
}

void DFS()
{
int i;
for(i = 0;i < VSIZE;i++)
vSetState[i] = WHITE;
/* for every vertex in vSet */
for(i = 0;i < VSIZE;i++)
DFS_visit(vSet[i]);
}

void DFS_visit(int vertex)
{
int i;
vSetState[vertex-1] = GRAY;/* dicovered */
/* for all the vertex's neighbors */
for(i = 0;i < VSIZE;i++)
if(graph[vertex-1][i] == 1 && vSetState[i] == WHITE)
DFS_visit(vSet[i]);
vSetState[vertex-1] = BLACK; /* visited */
printf("%d visited ! \n",vertex);
}

int main(int argc, char *argv[])
{
inputGraph();
dispGraph();
printf("___________________________\n");
DFS_visit(1);
printf("___________________________\n");
system("PAUSE");
return 0;
}

// 很久以前写的 ,对你应该有用
rottenapple 2006-01-06
  • 打赏
  • 举报
回复
搞错了,深度优先就是dfs,
这样做的话如果还要找最短路径,最长路经,是不是还需要用Dijkstra算法呢?
rottenapple 2006-01-06
  • 打赏
  • 举报
回复
深度优先不行吗?
rottenapple 2006-01-06
  • 打赏
  • 举报
回复
dfs怎样才能把所有路径都遍历一边呢?
谁给个算法例子阿。。着急
MadLee 2006-01-05
  • 打赏
  • 举报
回复
没环的话直接sort一下顶点就行了,有环的话所有路径就比较难定义了
Zephyrzzz 2006-01-05
  • 打赏
  • 举报
回复
邻接表+dfs直接回溯行了
boblaile 2006-01-05
  • 打赏
  • 举报
回复
2。列出时间最短路
可以用Dijkstra算法

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧