C++实现图的十字链表表示及相关各算法

本文详细介绍了如何使用C++通过十字链表来表示图,并探讨了基于十字链表的图相关算法,包括深度优先搜索(DFS)、广度优先搜索(BFS)以及最短路径求解等。通过对图结构的实现,读者可以更好地理解和应用图论在实际问题中的解决策略。
摘要由CSDN通过智能技术生成

/*
 * OLGraph.h
 *
 *  Created on: Oct 19, 2015
 *      Author: chris
 */

#ifndef OLGRAPH_H_
#define OLGRAPH_H_

#include<iostream>
#include<limits>

#define MAX_VERTEX_NUM 20
#define INF INT_MAX

struct InfoType{
	int weight;
	InfoType(): weight(0) {}
};

struct ArcBox{
	int from, to;
	InfoType *info;
	ArcBox *nextin, *nextout;
	ArcBox():from(0), to(0),
			info(NULL), nextin(NULL), nextout(NULL) {}
};

typedef int VertexType;

struct VexNode{
	VertexType data;
	ArcBox arcin, arcout;

	VexNode():data(0){}
};

struct OLGraph{
	VexNode* vexlist;
	int vexnum;

	OLGraph(): vexlist(NULL), vexnum(0) {}
};

struct Activity{
	int from, to;
	int dut;
	int ee, el;

	Activity(): from(0), to(0),
			dut(0), ee(0), el(0) {}
};

bool OLGraphCreate(OLGraph & G, int vexnum);
void OLGraphDestroy(OLGraph & G);
bool OLGraphBuild(OLGraph & G);

bool OLGraphAddArc(OLGraph & G, int from, int to, InfoType& info);
bool OLGraphGetArc(OLGraph & G, int from, int to, InfoType& info);
bool OLGraphChgArc(OLGraph & G, int from, int to, InfoType& info);
bool OLGraphDelArc(OLGraph & G, int from, int to);

int OLGraphInDeg(OLGraph & G, int vex);
int OLGraphOutDeg(OLGraph & G, int vex);

int OLGraphPreVex(OLGraph & G, int vex, int *& pre);
int OLGraphSucVex(OLGraph & G, int vex, int *& suc);

bool OLGraphGetDist(OLGraph & G, int from, int to, int& dist);

bool OLGraphDFSTraverse(OLGraph & G, int root, bool(*visit)(VexNode*));
bool OLGraphBFSTraverse(OLGraph & G, int root, bool(*visit)(VexNode*));

bool OLGraphLabelSCC(OLGraph & G, int *& scc_label);
bool OLGraphArticul(OLGraph & G, int & num, int *& articul);

bool OLGraphTopoSort(OLGraph & G, int*& result);
bool OLGraphTopoSort(OLGraph & G, int*& result, int*& ve);
bool OLGraphCriticalPath(OLGraph & G, int& num ,Activity*& crtpath);

bool OLGraphDijkstra(OLGraph & G, int src, int*& dist, int*& pre);
bool OLGraphBellmanFord(OLGraph & G, int src, int *& dist, int*& pre);
bool OLGraphWarshall(OLGraph & G, int**& dist, int**& pre);

void OLGraphWalkThrough(OLGraph & G);

#endif /* OLGRAPH_H_ */


/*
 * OLGraph.cpp
 *
 *  Created on: Oct 19, 2015
 *      Author: chris
 */

#include"OLGraph.h"
#include<iostream>

using namespace std;

bool OLGraphCreate(OLGraph & G, int vexnum)
{
	VexNode * temp = new VexNode[vexnum];
	if(!temp) return false;
	G.vexlist = temp;
	G.vexnum = vexnum;
	for(int i = 0; i < vexnum; ++i)
		G.vexlist[i].data = i;
	return true;
}

void OLGraphDestroy(OLGraph & G)
{
    if(!G.vexlist) return;
    for(int i = 0; i < G.vexnum; ++i) {
    	ArcBox * opcur = G.vexlist[i].arcout.nextout, *tp = NULL;
    	while(opcur) {
    		tp = opcur;
    		opcur = opcur->nextout;
    		delete tp->info;
    		delete tp;
    	}
    }
    delete G.vexlist;
	G.vexnum = 0;
}

bool OLGraphBuild(OLGraph & G)
{
	OLGraphDestroy(G);

	int vexnum = 0;
	cout << "Enter the number of vertices: "; cout.flush();
	cin >> vexnum;

	if(!OLGraphCreate(G, vexnum))
		return false;

	int arcnum = 0;
	cout << "Enter the number of arcs: "; cout.flush();
	cin >> arcnum;

	for(int i = 0; i < arcnum; ++i) {
		int from, to;
		InfoType info;
		cout << "Enter from, to, and info: "; cout.flush();
		cin >> from >> to >> info.weight;

		if(!OLGraphAddArc(G, from, to, info)) {
			cout << "Add Arc failed." << endl;
		}
	}//end for
	return true;
}

bool OLGraphAddArc(OLGraph & G, int from, int to, InfoType& info)
{
	//in range.
	if(from < 0 || from >= G.vexnum || to < 0 || to >= G.vexnum)
		return false;
	//avoid rep.
	if(OLGraphGetArc(G, from, to, info))
		return false;
	//alloc.
	ArcBox * arc = new ArcBox;
	if(!arc) return false;
	arc->info = new InfoType;
	if(!arc->info) {
		delete arc;
		return false;
	}
	//load.
	arc->from = from;
	arc->to = to;
	*arc->info = info;

	//insert;
	ArcBox * oprior = &G.vexlist[from].arcout,
		   * iprior = &G.vexlist[to].arcin;
	while(oprior->nextout && oprior->nextout->to < to)
		oprior = oprior->nextout;
	arc->nextout = oprior->nextout;
	oprior->nextout = arc;

	while(iprior->nextin && iprior->nextin->from < from)
		iprior = iprior->nextin;
	arc->nextin = iprior->nextin;
	iprior->nextin = arc;

	return true;
}

bool OLGraphGetArc(OLGraph & G, int from, int to, InfoType& info)
{
	//in range.
	if(from < 0 || from >= G.vexnum || to < 0 || to >= G.vexnum)
		return false;
	ArcBox * opcur = G.vexlist[from
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值