Beispiel #1
0
//图的创建函数
int GraphListCreat(AdjList *graph)
{
	int i, j;
	int leap = 0;
	int a[MAX_VERTEX_NUM ][MAX_VERTEX_NUM ];
	ArcNode *p;
	
	GraphRead(graph, a);
	for(i = 0 ; i < graph->vexnum ; i++){
		for(j = 0 ; j < graph->vexnum ; j++){
			if(i == j){
				continue;
			}
			if(leap == 0){
				graph->vertex[i].fristarc = NULL;
			}
			if(a[i][j] != 32768 && leap == 0){
				graph->vertex[i].fristarc = (ArcNode *) malloc (sizeof(ArcNode));
				graph->vertex[i].fristarc->adjvex = j;
				graph->vertex[i].fristarc->nextarc = NULL;
				leap = 1;
				continue;
			}
			if(a[i][j] != 32768){
				p = (ArcNode *) malloc (sizeof(ArcNode));
				p->adjvex = j;
				p->nextarc = graph->vertex[i].fristarc->nextarc;
				graph->vertex[i].fristarc->nextarc = p;
			}
		}
		leap = 0;
	}

	return 0;
}
Beispiel #2
0
int main()
{
	Graph* analysisGraph;
	FILE* f;
	char buffer[BUFFER_SIZE];

	printf("Please enter the file to read from: ");
	fgets(buffer, BUFFER_SIZE, stdin);
	buffer[strlen(buffer) - 1] = '\0';
	f = fopen(buffer, "r");
	if(f)
	{
		analysisGraph = GraphRead(f);
		if(!analysisGraph)
		{
			printf("Fatal error: Problem occurred when reading '%s'!\n", buffer);
			return -1;
		}
		if(!GraphFindTransitiveClosure(analysisGraph))
		{
			printf("Error: Graph is not directed! Transitive closure cannot be found.\n");
		}
		GraphWrite(analysisGraph, stdout);
		GraphFree(analysisGraph);
	}
	else
	{
		printf("Fatal error: Can't read file '%s'!\n", buffer);
	}
	fclose(f);
	return 0;
}
Beispiel #3
0
//迪杰斯特拉求最短路径
int GraphListPath_DJS(AdjList *graph, int f, int t)
{
	Queue *path;					//申请队列
	QueueDataElem *node, *p;			//申请队列元素节点
	MidNode *head, *mid;				//申请中转节点
	int min = 0, num = 0;				//记录节点的数值
	int i = 0;
	int weigth[MAX_VERTEX_NUM][MAX_VERTEX_NUM];	//存放临接矩阵
	int donevex[MAX_VERTEX_NUM] = {0};		//存放已经走过的节点
	ListNode LiNo[MAX_VERTEX_NUM];			//存放数字信息
	ArcNode *GrNo;					//申请末尾节点
	List *h;

	GraphRead(graph, weigth);			//读取临接矩阵
	path = (Queue *) malloc (sizeof(Queue));
	p = (QueueDataElem *) malloc (sizeof(QueueDataElem));
	GrNo = (ArcNode *) malloc (sizeof(ArcNode));
	node = (QueueDataElem *) malloc (sizeof(QueueDataElem));	//初始化
	donevex[f] = 1;					//置初始访问路径
	QueueInit(path);				//初始化队列
	node->l = 0;					
	node->vn = f;
	node->datanext = NULL;				//置初始节点的next为NULL
	QueueEnter(path, *node);			//初始节点入队
	while(i < graph->vexnum - 1){			//进入循环
		min = 32768;
		mid = path->front->next;		
		node = &(mid->s);			//去队列头元素
		while(1){
			GrNo = graph->vertex[node->vn].fristarc;	//得到临接表形态的头指针
			while(GrNo != NULL){				
				if((weigth[node->vn][GrNo->adjvex] + node->l) < min && donevex[GrNo->adjvex] != 1){
					min = weigth[node->vn][GrNo->adjvex] + node->l;
					num = GrNo->adjvex;
					LiNo[GrNo->adjvex].deep = node->vn;
				}
				GrNo = GrNo->nextarc;	//邻接表的下一项
			}
			mid = mid->next;		//判断队列是否为空
			if(mid == NULL){
				break;
			}
			node = &(mid->s);
		}
		node = (QueueDataElem *) malloc (sizeof(QueueDataElem));
		node->l = min;
		node->vn = num;
		donevex[num] = 1;
		mid = path->front->next;				//指向初始路径
		p = &(mid->s);
		while(1){
			if(p->vn == LiNo[node->vn].deep){
				break;
			}
			mid = mid->next;
			if(mid == NULL){
				break;
			}
			p = &(mid->s);
		}
		node->datanext = p;
		QueueEnter(path, *node);				//新路径入队
		i++;
	}
	head = path->front;
	while(head != NULL){						//头不空则往下找节点
		if(head->s.vn == t){
			break;
		}
		head = head->next;
	}
	*node = head->s;
	h = (List *)malloc(sizeof(List));
	ListInit(h);
	while(node != NULL){						
//		printf("%d\t", node->vn + 1);
//		node = node->datanext;
		ListInsert(h, *node);
		node = node->datanext;
	}
	ListDxu(h);
	h = h->next;
	while(h != NULL){
		printf("%d\t", h->data.vn);
		h = h->next;
	}
	printf("\n");
	return 0;
}