main.h:
#include
#include
#define DefaultSize 10
#define maxWeight -1
using namespace std;
template
struct Edge
{
int dest;
E cost;
Edge *link;
Edge(int d=0,int c=0):dest(d),cost(c),link(NULL){}
};
template
struct Vertex
{
T data;
Edge *adj;
};
template
class Base
{
public:
virtual T getValue(int i)=0;
virtual E getWeight(int v1,int v2)=0;
virtual bool insertVertex(const T& vertex)=0;
virtual bool insertEdge(const T& l,const T& r,E cost)=0;
virtual void Show()=0;
virtual bool removeEdge(int v1,int v2)=0;
virtual bool removeVertex(int v)=0;
virtual int getFirstNeighbor(int v)=0;
virtual int NumberOfEdge()=0;
virtual int getNextNeighbor(int v,int w)=0;
};
template
class Graphlnk:public Base
{
public:
Graphlnk(int sz=DefaultSize)
{
numVertices=0;
maxVertices=DefaultSize;
numEgde=0;
NodeTable = new Vertex[sz];
for(int i=0;i=numVertices)return -1;
Edge *p = NodeTable[v].adj;
if(p!=NULL)
{
return p->dest;
}
return -1;
}
int getNextNeighbor(int v,int w)
{
if(v<0 || v>=numVertices || w<0 || w>=numVertices)
{
return -1;
}
Edge *p = NodeTable[v].adj;
while(p!=NULL)
{
if(p->dest == w)
{
if(p->link!=NULL)
return p->link->dest;
}
p = p->link;
}
return -1;
}
bool removeVertex(int v)
{
Edge *p = NodeTable[v].adj;
while(p!=NULL)
{
removeEdge(v,p->dest);
p=p->link;
}
NodeTable[v].data = NodeTable[numVertices-1].data;
NodeTable[v].adj = NodeTable[numVertices-1].adj;
p = NodeTable[numVertices-1].adj;
Edge *q = NULL;
while(p!=NULL)
{
int index = p->dest;
q = NodeTable[index].adj;
while(q!=NULL)
{
if(q->dest==(numVertices-1))
{
q->dest = v;
break;
}
q=q->link;
}
p=p->link;
}
numVertices--;
}
bool removeEdge(int v1,int v2)
{
if(v1<0||v1>=numVertices||v2<0||v2>=numVertices)
{return false;}
Edge *p = NodeTable[v1].adj;
Edge *q = NULL;
while(p!=NULL && p->dest!=v2)
{
q=p;
p=p->link;
}
if(q==NULL && p==NULL)
{
return false;
}
if(q==NULL && p!=NULL && p->dest==v2)
{
NodeTable[v1].adj=p->link;
delete p;
}
if(p==NULL)
{
return false;
}
else if(q!=NULL)
{
q->link=p->link;
delete p;
}
p = NodeTable[v2].adj;
q = NULL;
while(p!=NULL && p->dest!=v1)
{
q=p;
p=p->link;
}
if(q==NULL && p==NULL)
{
return false;
}
if(q==NULL && p!=NULL && p->dest==v1)
{
NodeTable[v2].adj=p->link;
delete p;
}
if(p==NULL)
{
return false;
}
else if(q!=NULL)
{
q->link=p->link;
delete p;
}
numEgde--;
}
bool insertEdge(const T& l,const T& r,E cost)
{
int v1 = getValuePos(l);
int v2 = getValuePos(r);
Edge *p = NodeTable[v1].adj;
Edge *s = new Edge(v2,cost);
Edge *q = NULL;
while(p!=NULL && p->dest!=cost)
{
q=p;
p=p->link;
}
if(p!=NULL && p->dest==cost)
{
return false;
}
if(q==NULL)
{
NodeTable[v1].adj = s;
}
if(p==NULL && q!=NULL)
{
s->link = NodeTable[v1].adj;
NodeTable[v1].adj=s;
}
p = NodeTable[v2].adj;
s = new Edge(v1,cost);
q = NULL;
while(p!=NULL && p->dest!=cost)
{
q=p;
p=p->link;
}
if(p!=NULL && p->dest==cost)
{
return false;
}
if(q==NULL)
{
NodeTable[v2].adj = s;
}
if(p==NULL&&q!=NULL)
{
s->link = NodeTable[v2].adj;
NodeTable[v2].adj=s;
}
numEgde++;
}
bool insertVertex(const T& vertex)
{
NodeTable[numVertices++].data=vertex;
return true;
}
void Show()
{
Edge *p=NULL;
for(int i=0;i ";
p=NodeTable[i].adj;
while(p!=NULL)
{
cout<dest<<"--"<cost<<" ";
p=p->link;
}
cout<=numVertices || v2<0 || v2>=numVertices)
{
return -1;
}
Edge *p = NodeTable[v1].adj;
// Edge *m = NULL;
while(p!=NULL)
{
//m = p;
if(p->dest==v2)
break;
p=p->link;
}
if(p!=NULL)
{return p->cost;}
else
{return maxWeight;}
}
T getValue(int i)
{
return NodeTable[i].data;
}
int NumberOfVertices()
{
return numVertices;
}
int getValuePos(const T &t)
{
for(int i=0;i * getNodeptr(int i)
{
return NodeTable[i].adj;
}
private:
int numVertices;
int maxVertices;
int numEgde;
Vertex *NodeTable;
};
template
void DFS(Graphlnk& G,const T& v,bool visted[])
{
int index = G.getValuePos(v);
cout<
void DFS(Graphlnk& G,const T& v)
{
int n = G.NumberOfVertices();
bool *visted = new bool[n];
for(int i=0;i
void BFS(Graphlnk& G,const T& v)
{
int n = G.NumberOfVertices();
bool *visted = new bool[n];
for(int i=0;i Q;
Q.push(i);
int index;
while(!Q.empty())
{
index = Q.front();
Q.pop();
if(!visted[index])
{
cout<
void Search(Graphlnk &G)
{
bool *visted = new bool[G.NumberOfVertices()];
for(int i=0;i
main.cpp:
#include
#include "main.h"
using namespace std;
template
class MinHeap
{
public:
MinHeap(int sz)
{
Maxsize = sz;
data = new T[sz];
Numsize = 0;
}
void Insert(T x)
{
data[Numsize++] = x;
int n = Numsize/2;
while(n>=0)
{
Sort(data,n);
n--;
}
}
void Swap(T *a,T *b)
{
T temp = *a;
*a = *b;
*b = temp;
}
T Remove()
{
T temp = data[0];
int n=Numsize;
Numsize=0;
for(int i=1;ia[j+1])
j=j+1;
if(ja[j])
Swap(&a[i],&a[j]);
i = j;
j = 2*i+1;
}
}
void Show()
{
for(int i=0;iy)
{
parent[x]+=parent[y];
parent[y]=x;
}
else
{
parent[y]+=parent[x];
parent[x]=y;
}
}
}
int Find(int x)
{
while(parent[x]>0)
x=parent[x];
return x;
}
private:
int *parent;
int size;
};
#define Default -1
const float maxValue = Default;
template
struct MSTEdgeNode
{
int tail,head;E key;
MSTEdgeNode():tail(-1),head(-1),key(0){}
bool operator > (const MSTEdgeNode &MST)
{
return key>MST.key;
}
bool operator < (const MSTEdgeNode &MST)
{
return key
class MinSpanTree
{
protected:
MSTEdgeNode *edgevalue;
int MaxSize,n;
public:
MinSpanTree(int sz=Default-1):MaxSize(sz),n(0)
{
edgevalue = new MSTEdgeNode[sz];
}
int Insert(MSTEdgeNode& item)
{
edgevalue[n++]=item;
}
void Show()
{
for(int i=0;i
void Kruskal(Graphlnk& G,MinSpanTree &MST)
{
MSTEdgeNode ed;int u,v,count;
int n = G.NumberOfVertices();
int m = G.NumberOfEdge();
MinHeap >H(m);
UFset F(n);
for(u = 0;u mst(10);
Graphlnk gh;
gh.insertVertex('A');
gh.insertVertex('B');
gh.insertVertex('C');
gh.insertVertex('D');
gh.insertVertex('E');
gh.insertVertex('F');
gh.insertVertex('G');
gh.insertEdge('A','B',28);
gh.insertEdge('B','C',16);
gh.insertEdge('C','D',12);
gh.insertEdge('D','E',22);
gh.insertEdge('E','F',25);
gh.insertEdge('F','A',10);
gh.insertEdge('B','G',14);
gh.insertEdge('D','G',18);
gh.insertEdge('E','G',24);
gh.Show();
cout<<"-------------------"<