深度优先搜索发现时间发现但不打印 [英] Depth First Search discovery time finding but not printing

查看:101
本文介绍了深度优先搜索发现时间发现但不打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai ...我编写了一个执行深度优先搜索的程序,代码如下所示,但是在DFS_VISIT()函数内部发现时间正确打印,但是从主函数来看所有节点的打印时间均为0 .

Hai...i wrote a program to perform depth first search.The code is given below.But inside the DFS_VISIT() function the discovery time is printing correctly.But from the main function it is printing as 0 for all nodes.

    v[u]->discovery=++time;
printf("\nDiscovery of %d:%d",u,v[u]->discovery);



我可以如上所述从DFS_visit()打印v [u]->发现.bt从主函数进行相同的打印.* v []被声明为全局.通过* v []可访问的所有其他元素正在打印正确地来自main.

我不知道这种情况.请帮助我.下面提供了完整的代码.



i can print v[u]->discovery from the DFS_visit() as above.bt the same dosnt print from main function.*v[] is declared as global.all other elements which is accessible via *v[] is printing correctly from main.

I am not gettg y this happening.Sombody please help me.Full code is given below.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<string.h>
struct node
{
	int item;
	char color[5];
	int discovery,finishing,parent;
	struct node *next;
};
struct graph
{
 int x,y;
}graph[10];
struct node *nwptr=NULL,*v[10],*temp=NULL;
int time=0,n;
void DFS();
void DFS_VISIT(int);
int main()
{
	int a,i,j,temp1,temp2,x,y,k;
	char string[10];
	int gdriver=DETECT,gmode;
	clrscr();
	printf("\nEnter the no. vertex :");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
	    v[i]=(struct node*)malloc(sizeof(struct node));
	    printf("\nEnter the value of vertex   :");
	    scanf("%d",&v[i]->item);
	    v[i]->next=NULL;
	    printf("\nEnter the no. of adj vertex :");
	    scanf("%d",&a);
	    for(j=1;j<=a;j++)
	    {
	       nwptr=(struct node*)malloc(sizeof(struct node));
	       printf("\nEnter the value of adj vertex :");
	       scanf("%d",&nwptr->item);
	       nwptr->next=NULL;
	       for(temp=v[i];temp->next!=NULL;temp=temp->next);
		 temp->next=nwptr;
	    }
	}
	printf("\n      ADJACENCY LIST\n");
	for(i=1;i<=n;i++)
	{
	    printf("\n%d",v[i]->item);
	    for(temp=v[i]->next;temp!=NULL;temp=temp->next)
	      printf("->%d",temp->item);
	}
	printf("\nPress any key to continue...\n");
	getch();
	initgraph(&gdriver,&gmode,"E:\\ProGRAMZ\\TC\\BGI");
	temp1=n/2;
	temp2=n-temp1;
	setfillstyle(SOLID_FILL,WHITE);
	k=50;
	for(i=1;i<=temp1;i++)
	{
	    fillellipse(k,50,10,10);
	    graph[i].x=k;
	    graph[i].y=50;
	    k+=50;
	}
	k=50;
	for(j=1;j<=temp2;j++,i++)
	{
	    fillellipse(k,100,10,10);
	    graph[i].x=k;
	    graph[i].y=100;
	    k+=50;
	}
	for(i=1;i<=n;i++)
	{

	    for(temp=v[i]->next;temp!=NULL;temp=temp->next)
	       line(graph[i].x,graph[i].y,graph[temp->item].x,graph[temp->item].y);
	    itoa(v[i]->item,string,10);
	    setcolor(0);
	    outtextxy(graph[i].x-3,graph[i].y-3,string);
	    setcolor(15);
	}
	getch();
	DFS();
	outtextxy(10,700,"\nPress any key to continue...\n");
	getch();
	cleardevice();
	printf("\nNODE\tDISCOVERY\tFINISHING\tPARENT\tCOLOR\n");
	for(i=1;i<=n;i++)
	printf("\n%d\t%d\t\t%d\t\t%d\t%s",v[i]->item,v[i]->discovery,v[i]->finishing,v[i]->parent,v[i]->color);
	getch();
	return(0);
}
void DFS()
{
	int i;
	for(i=1;i<=n;i++)
	{
	  strcpy(v[i]->color,"WHITE");
	  v[i]->discovery=NULL;
	  v[i]->finishing=NULL;
	  v[i]->parent=NULL;
	}
	time=0;
	for(i=1;i<=n;i++)
	{
	  if(strcmp(v[i]->color,"WHITE")==0)
	     DFS_VISIT(v[i]->item);
	}
}
void DFS_VISIT(int u)
{
	strcpy(v[u]->color,"GRAY");
	v[u]->discovery=++time;
	printf("\nDiscovery of %d:%d",u,v[u]->discovery);
	for(temp=v[u]->next;temp!=NULL;temp=temp->next)
	{
	   if(strcmp(v[temp->item]->color,"WHITE")==0)
	   {
	      v[temp->item]->parent=u;
	      DFS_VISIT(temp->item);
	   }
	}
	strcpy(v[u]->color,"BLACK");
	v[u]->finishing=++time;
}

推荐答案

这当然不容易弄清楚,您在节点数组中使用最差值似乎过于复杂,并且可能充满危险.您还在函数中使用静态变量,而不是将其作为参数传递,这也是一个坏主意.

我唯一能看到的是您在打印节点数据之前在main()中调用了函数DFS(),但是DFS()似乎删除了节点数据.
This is certainly not easy to figure out, your use of offest values into the array of nodes seems overly complex, and probably fraught with danger. You are also using static variables within your functions rather than passing them as parameters, again a bad idea.

The only thing I could see was that you call function DFS() in main() before printing node data, but DFS() appears to delete the node data.


这篇关于深度优先搜索发现时间发现但不打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆