数据结构造轮子4.1-----图的十字链表表示以及相关算法(拓步排序)

Graph.h

#pragma once

#include "iostream"

#define MAX_VERTEX_NUM 20
#define INFINITY 9999

using namespace std;

/*
	二维数组表示的无向图
*/
class Graph_Using_Array
{
public:
	/*
		邻接矩阵
	*/
	int matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
	int arcnum;
	int vexnum;

	/*
		为kruskal算法实现方便此处创建一边数据数组
	*/
	struct arc
	{
		int x0, x1;
		int weight;
	}*arcs;

	void InitGraph();
	void CreateGraph();

	/*
		最短路径
	*/
	void Dijstra(int start_point);
	void Floyd(int start_point);

	/*
		最小生成树
	*/
	void Prim(int start_point);
	void Kruskal();
};

class Graph_Using_Adjacency_List
{
public:
	/*
		边节点
	*/
	typedef struct arc_node
	{
		int start_vex;
		int end_vex;

		int weight;

		arc_node * next_in_arc_node;
		arc_node * next_out_arc_node;
	}arc_node;

	/*
		表头节点
	*/
	typedef struct vex_node
	{
		int vex;
		arc_node * first_in_arc_node;
		arc_node * first_out_arc_node;
	}vex_node;

	/*
		图数据
	*/
	vex_node * graph;

	int vexnum;
	int arcnum;

	void InitGraph();
	void CreateGraph();
	void TopologicalSort();
};

Graph_Using_Adjacency_List.cpp

#include "stdafx.h"
#include "Graph.h"
#include "iostream"
#include "Stack.h"

using namespace std;

void Graph_Using_Adjacency_List::InitGraph()
{	
	graph = new vex_node[MAX_VERTEX_NUM];
	arcnum = 0;
	vexnum = 0;
	int i;
	for (i = 0;i < MAX_VERTEX_NUM;i++)
	{
		graph[i].first_in_arc_node = NULL;
		graph[i].first_out_arc_node = NULL;
	}
}

/*
	创建十字链表
*/
void Graph_Using_Adjacency_List::CreateGraph()
{
	cout << "arcnum:";
	cin >> arcnum;
	cout << "vexnum:";
	cin >> vexnum;

	int i;
	int x0, x1, weight;
	arc_node * this_arc_node;
	for (i = 0;i < arcnum;i++)
	{
		cout << "x0,x1,weight:";
		cin >> x0 >> x1 >> weight;

		/*
			存入数据并链接
		*/
		this_arc_node = (arc_node *)malloc(sizeof(arc_node));
		this_arc_node->start_vex = x0;
		this_arc_node->end_vex = x1;
		this_arc_node->next_in_arc_node = graph[x1].first_in_arc_node;
		this_arc_node->next_out_arc_node = graph[x0].first_out_arc_node;
		graph[x1].first_in_arc_node = this_arc_node;
		graph[x0].first_out_arc_node = this_arc_node;
	}
}

void Graph_Using_Adjacency_List::TopologicalSort()
{
	/*
		存放入度
	*/
	int in_degree[MAX_VERTEX_NUM];
	int i, j;

	/*
		计算每个顶点入度
	*/
	arc_node * mark_arc_node;
	for (i = 0;i < MAX_VERTEX_NUM;i++)
	{
		j = 0;
		/*
			当该点是孤立点时
		*/
		if (!graph[i].first_in_arc_node && !graph[i].first_out_arc_node)
		{
			in_degree[i] = -1;
			continue;
		}
		for (mark_arc_node = graph[i].first_in_arc_node;mark_arc_node != NULL;mark_arc_node = mark_arc_node->next_in_arc_node, j++);
		in_degree[i] = j;
	}

	Stack_Using_Array stack;
	stack.InitStack();

	for (i = 0;i < MAX_VERTEX_NUM;i++)
	{
		if (in_degree[i] == 0)
		{
			stack.Push(i);
			cout << "   " << i << "   ";
		}
	}

	while (!stack.isEmpty())
	{
		stack.Pop(j);
		for (mark_arc_node = graph[j].first_out_arc_node;mark_arc_node != NULL;mark_arc_node = mark_arc_node->next_out_arc_node)
		{
			if (!--in_degree[mark_arc_node->end_vex])
			{
				stack.Push(mark_arc_node->end_vex);
				cout << "   " << mark_arc_node->end_vex << "   ";
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值