/*
* 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