2021-12-09 14:58:46 +00:00
|
|
|
|
#ifndef _graph_hpp_
|
|
|
|
|
#define _graph_hpp_
|
2021-12-17 07:37:48 +00:00
|
|
|
|
#include "../ex10/queue.hpp"
|
2021-12-09 14:58:46 +00:00
|
|
|
|
#include "vector.hpp"
|
2021-12-10 01:20:41 +00:00
|
|
|
|
#include <iomanip>
|
2021-12-09 14:58:46 +00:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
2021-12-17 07:37:48 +00:00
|
|
|
|
#define DISCOVERED 1
|
|
|
|
|
#define UNDISCOVERED 0
|
|
|
|
|
#define VISITED 2
|
2021-12-09 14:58:46 +00:00
|
|
|
|
|
|
|
|
|
template <class Te>
|
|
|
|
|
class Edge
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Te data;
|
|
|
|
|
int weight;
|
|
|
|
|
Edge()
|
|
|
|
|
{
|
|
|
|
|
weight = 0;
|
|
|
|
|
}
|
|
|
|
|
Edge(int w)
|
|
|
|
|
{
|
|
|
|
|
weight = w;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Edge(Te d, int w)
|
|
|
|
|
{
|
|
|
|
|
data = d;
|
|
|
|
|
weight = w;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class Tv>
|
|
|
|
|
class Vertex
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Tv data;
|
|
|
|
|
int inDegree;
|
|
|
|
|
int outDegree;
|
2021-12-17 07:37:48 +00:00
|
|
|
|
int status;
|
2021-12-09 14:58:46 +00:00
|
|
|
|
Vertex()
|
|
|
|
|
{
|
|
|
|
|
// data = NULL;
|
|
|
|
|
inDegree = 0;
|
|
|
|
|
outDegree = 0;
|
2021-12-17 07:37:48 +00:00
|
|
|
|
status = UNDISCOVERED;
|
2021-12-09 14:58:46 +00:00
|
|
|
|
}
|
|
|
|
|
Vertex(Tv d)
|
|
|
|
|
{
|
|
|
|
|
data = d;
|
|
|
|
|
inDegree = 0;
|
|
|
|
|
outDegree = 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class Te, class Tv>
|
|
|
|
|
class graphVertex
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
Vector<Vertex<Tv> *> V;
|
2021-12-10 08:51:43 +00:00
|
|
|
|
Edge<Te> *E[100][100] = {NULL};
|
2021-12-17 07:37:48 +00:00
|
|
|
|
|
2021-12-09 14:58:46 +00:00
|
|
|
|
int v;
|
|
|
|
|
int e;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
graphVertex()
|
|
|
|
|
{
|
|
|
|
|
v = 0;
|
|
|
|
|
e = 0;
|
|
|
|
|
}
|
|
|
|
|
~graphVertex()
|
|
|
|
|
{
|
|
|
|
|
int n = v;
|
|
|
|
|
for (int j = 0; j < n; j++)
|
|
|
|
|
for (int k = 0; k < n; k++)
|
|
|
|
|
delete E[j][k];
|
|
|
|
|
for (int j = 0; j < n; j++)
|
|
|
|
|
delete V[j];
|
|
|
|
|
}
|
2021-12-09 15:04:48 +00:00
|
|
|
|
virtual Tv vertex(int i)
|
2021-12-09 14:58:46 +00:00
|
|
|
|
{
|
|
|
|
|
return V[i]->data;
|
|
|
|
|
}
|
|
|
|
|
virtual int inDegree(int i)
|
|
|
|
|
{
|
|
|
|
|
return V[i]->inDegree;
|
|
|
|
|
}
|
|
|
|
|
virtual int outDegree(int i)
|
|
|
|
|
{
|
|
|
|
|
return V[i]->outDegree;
|
|
|
|
|
}
|
|
|
|
|
virtual int insect(int id, Tv value)
|
|
|
|
|
{
|
2021-12-09 16:34:32 +00:00
|
|
|
|
// if (V[id] == NULL)
|
|
|
|
|
// {
|
|
|
|
|
Vertex<Tv> *temp1 = new Vertex<Tv>(value);
|
|
|
|
|
V.put(id, temp1);
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// V[id]->data = value;
|
2021-12-09 14:58:46 +00:00
|
|
|
|
v++;
|
|
|
|
|
return id;
|
|
|
|
|
}
|
2021-12-09 16:34:32 +00:00
|
|
|
|
virtual Tv remove(int id)
|
2021-12-09 14:58:46 +00:00
|
|
|
|
{
|
2021-12-17 07:37:48 +00:00
|
|
|
|
if (V[id] == NULL)
|
2021-12-10 08:51:43 +00:00
|
|
|
|
return (Tv)0;
|
2021-12-09 14:58:46 +00:00
|
|
|
|
for (int j = 0; j < v; j++)
|
|
|
|
|
{
|
2021-12-10 01:20:41 +00:00
|
|
|
|
if (E[id][j] != NULL)
|
2021-12-09 14:58:46 +00:00
|
|
|
|
{
|
|
|
|
|
delete E[id][j];
|
2021-12-10 08:51:43 +00:00
|
|
|
|
E[id][j] = NULL;
|
2021-12-09 14:58:46 +00:00
|
|
|
|
V[j]->inDegree--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
v--;
|
2021-12-09 15:04:48 +00:00
|
|
|
|
Tv temp = vertex(id);
|
2021-12-09 14:58:46 +00:00
|
|
|
|
delete V[id];
|
2021-12-09 15:04:48 +00:00
|
|
|
|
V.put(id, NULL);
|
2021-12-09 16:34:32 +00:00
|
|
|
|
return temp;
|
2021-12-09 14:58:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// id1->id2 进行连接
|
|
|
|
|
// return:
|
|
|
|
|
// -1 顶点不存在
|
|
|
|
|
// -2 边已存在
|
|
|
|
|
// 0 成功
|
|
|
|
|
virtual int link(int id1, int id2, int w)
|
|
|
|
|
{
|
2021-12-09 16:38:46 +00:00
|
|
|
|
if (V[id1] == NULL || V[id2] == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
if (E[id1][id2] != NULL)
|
|
|
|
|
return -2;
|
2021-12-09 14:58:46 +00:00
|
|
|
|
V[id1]->outDegree++;
|
|
|
|
|
V[id2]->inDegree++;
|
2021-12-10 01:20:41 +00:00
|
|
|
|
e++;
|
2021-12-09 16:34:32 +00:00
|
|
|
|
Edge<Te> *temp = new Edge<Te>(w);
|
2021-12-17 07:37:48 +00:00
|
|
|
|
E[id1][id2] = temp;
|
2021-12-09 14:58:46 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// 解除 id1->id2 连接
|
|
|
|
|
// return:
|
|
|
|
|
// -1 顶点不存在
|
|
|
|
|
// -2 边不存在
|
|
|
|
|
// 0 成功
|
2021-12-09 15:22:41 +00:00
|
|
|
|
virtual int unlink(int id1, int id2)
|
2021-12-09 14:58:46 +00:00
|
|
|
|
{
|
|
|
|
|
if (V[id1] == NULL || V[id2] == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2021-12-10 08:51:43 +00:00
|
|
|
|
if (E[id1][id2] == NULL)
|
2021-12-09 14:58:46 +00:00
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
|
|
V[id1]->outDegree--;
|
|
|
|
|
V[id2]->inDegree--;
|
|
|
|
|
|
|
|
|
|
delete E[id1][id2];
|
2021-12-17 07:37:48 +00:00
|
|
|
|
E[id1][id2] = NULL;
|
2021-12-10 01:20:41 +00:00
|
|
|
|
e--;
|
2021-12-09 14:58:46 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-12-10 01:20:41 +00:00
|
|
|
|
|
|
|
|
|
virtual void display()
|
2021-12-17 07:37:48 +00:00
|
|
|
|
{
|
2021-12-10 08:51:43 +00:00
|
|
|
|
// std::cout << std::setw(14) << temp->weight;
|
|
|
|
|
//第一行
|
2021-12-10 01:29:13 +00:00
|
|
|
|
std::cout << std::left << std::setw(14) << " ";
|
2021-12-10 01:20:41 +00:00
|
|
|
|
for (int i = 0; i < V._len; i++)
|
|
|
|
|
if (V[i] != NULL)
|
|
|
|
|
std::cout << std::setw(4) << i << std::setw(10) << V[i]->data;
|
|
|
|
|
printf("\n");
|
2021-12-17 07:37:48 +00:00
|
|
|
|
|
2021-12-10 01:20:41 +00:00
|
|
|
|
for (int i = 0; i < V._len; i++)
|
|
|
|
|
if (V[i] != NULL)
|
|
|
|
|
{
|
2021-12-10 08:51:43 +00:00
|
|
|
|
//每行第一个
|
2021-12-10 01:20:41 +00:00
|
|
|
|
std::cout << std::setw(4) << i << std::setw(10) << V[i]->data;
|
2021-12-10 08:51:43 +00:00
|
|
|
|
|
|
|
|
|
//后面的
|
2021-12-10 01:29:13 +00:00
|
|
|
|
for (int j = 0; j < V._len; j++)
|
|
|
|
|
if (V[j] != NULL)
|
|
|
|
|
{
|
2021-12-10 08:51:43 +00:00
|
|
|
|
if (E[i][j] != NULL)
|
2021-12-10 01:29:13 +00:00
|
|
|
|
std::cout << std::setw(14) << E[i][j]->weight;
|
2021-12-10 08:51:43 +00:00
|
|
|
|
else
|
|
|
|
|
std::cout << std::setw(14) << "-";
|
2021-12-10 01:29:13 +00:00
|
|
|
|
}
|
2021-12-10 01:20:41 +00:00
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-10 09:08:32 +00:00
|
|
|
|
|
|
|
|
|
virtual void BFS(int id)
|
|
|
|
|
{
|
2021-12-17 07:37:48 +00:00
|
|
|
|
reset();
|
|
|
|
|
|
|
|
|
|
if(V[id]==NULL)
|
|
|
|
|
return;
|
2021-12-10 09:08:32 +00:00
|
|
|
|
Queue<int> q;
|
2021-12-17 07:37:48 +00:00
|
|
|
|
V[id]->status = DISCOVERED;
|
2021-12-10 09:08:32 +00:00
|
|
|
|
q.enqueue(id);
|
|
|
|
|
while (!q.empty())
|
|
|
|
|
{
|
2021-12-17 07:37:48 +00:00
|
|
|
|
int vid = q.dequeue();
|
2021-12-10 09:08:32 +00:00
|
|
|
|
|
2021-12-17 07:37:48 +00:00
|
|
|
|
for (int j = 0; j < 100; j++)
|
|
|
|
|
{
|
|
|
|
|
if (E[vid][j] != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (V[j]->status == UNDISCOVERED)
|
|
|
|
|
{
|
|
|
|
|
q.enqueue(j);
|
|
|
|
|
V[j]->status = DISCOVERED;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-10 09:08:32 +00:00
|
|
|
|
|
2021-12-17 07:37:48 +00:00
|
|
|
|
// std::cout << node->pos;
|
|
|
|
|
printf("VISIT ID: %d \n", vid);
|
|
|
|
|
V[vid]->status = VISITED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void reset()
|
|
|
|
|
{
|
|
|
|
|
for (int j = 0; j < V._len; j++)
|
|
|
|
|
{
|
|
|
|
|
if (V[j] != NULL)
|
|
|
|
|
{
|
2021-12-10 09:08:32 +00:00
|
|
|
|
|
2021-12-17 07:37:48 +00:00
|
|
|
|
V[j]->status = UNDISCOVERED;
|
|
|
|
|
}
|
2021-12-10 09:08:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-09 14:58:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|