邻接矩阵类写完基础功能,邻接表类修复已存在连接判断bug
This commit is contained in:
@@ -4,3 +4,4 @@ project(homework5)
|
|||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
add_executable(test2 test2.cpp)
|
add_executable(test2 test2.cpp)
|
||||||
|
add_executable(test1 test1.cpp)
|
||||||
|
|||||||
+59
-50
@@ -1,7 +1,4 @@
|
|||||||
//
|
// 邻接表类
|
||||||
// Created by 31416 on 24-12-4.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef LIST_GRAPH_H
|
#ifndef LIST_GRAPH_H
|
||||||
#define LIST_GRAPH_H
|
#define LIST_GRAPH_H
|
||||||
|
|
||||||
@@ -28,95 +25,107 @@ using namespace std;
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
class ListGraph {
|
class ListGraph {
|
||||||
private:
|
private:
|
||||||
struct node{
|
struct node {
|
||||||
T data;
|
T data;
|
||||||
T weight;
|
T weight;
|
||||||
struct node* next;
|
struct node *next;
|
||||||
};
|
};
|
||||||
vector<node*> nodes;
|
|
||||||
|
vector<node *> nodes;
|
||||||
bool flag; //定义是否有向
|
bool flag; //定义是否有向
|
||||||
bool isWeighted; //定义是否有权
|
bool isWeighted; //定义是否有权
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ListGraph(bool flag=false,bool isWeighted=false) {
|
ListGraph(bool flag = false, bool isWeighted = false) {
|
||||||
nodes.clear();
|
nodes.clear();
|
||||||
flag = false;
|
flag = false;
|
||||||
isWeighted = false;
|
isWeighted = false;
|
||||||
this->flag = flag;
|
this->flag = flag;
|
||||||
this->isWeighted = isWeighted;
|
this->isWeighted = isWeighted;
|
||||||
}
|
}
|
||||||
|
|
||||||
void insert(T data) {
|
void insert(T data) {
|
||||||
node* insert = new node();
|
node *insert = new node();
|
||||||
insert->data=nodes.size();
|
insert->data = nodes.size();
|
||||||
insert->weight = 0;
|
insert->weight = 0;
|
||||||
insert->next=new node();
|
insert->next = new node();
|
||||||
insert->next->data=data;
|
insert->next->data = data;
|
||||||
insert->next->next=NULL;
|
insert->next->next = NULL;
|
||||||
nodes.push_back(insert);
|
nodes.push_back(insert);
|
||||||
}
|
}
|
||||||
void connect(int id1,int id2,T weight=0) { //flag区分是否为有向图
|
|
||||||
if ((id1>nodes.size()-1||id2>nodes.size()-1)||(id1<0)||(id2<0)) {
|
void connect(int id1, int id2, T weight = 0) {
|
||||||
throw NodeIdOutOfRangeException("您提供的id超出范围",0X002);
|
//flag区分是否为有向图
|
||||||
|
if ((id1 > nodes.size() - 1 || id2 > nodes.size() - 1) || (id1 < 0) || (id2 < 0)) {
|
||||||
|
throw NodeIdOutOfRangeException("您提供的id超出范围", 0X002);
|
||||||
}
|
}
|
||||||
if(id1==id2) {
|
if (id1 == id2) {
|
||||||
throw SameNodeConnectException("禁止连接图中的同一个点",0x001);
|
throw SameNodeConnectException("禁止连接图中的同一个点", 0x001);
|
||||||
}
|
}
|
||||||
node *current = nodes.at(id1);
|
node *current = nodes.at(id1);
|
||||||
//跳过id节点和值节点
|
//跳过id节点和值节点
|
||||||
for(int i=0;i<1;i++) {
|
for (int i = 0; i < 1; i++) {
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
while(current->next!=NULL) {
|
while (current->next != NULL) {
|
||||||
if(current->next->data==id2) {
|
if (current->next->data == id2) {
|
||||||
if (current->next->weight==weight) {
|
if (current->next->weight == weight) {
|
||||||
throw InsertExistedConnectException("禁止插入已存在的连接",0x003);
|
throw InsertExistedConnectException("禁止插入已存在的连接", 0x003);
|
||||||
}
|
}
|
||||||
current->next->weight=weight;
|
|
||||||
}
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
current->next=new node();
|
|
||||||
current->next->data=id2;
|
|
||||||
current->next->weight = weight;
|
current->next->weight = weight;
|
||||||
current->next->next=NULL;
|
if (!flag) {
|
||||||
|
node *current2 = nodes.at(id2);
|
||||||
|
while (current2->next != NULL) {
|
||||||
|
current2 = current2->next;
|
||||||
|
}
|
||||||
|
current2->next->weight = weight;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
current->next = new node();
|
||||||
|
current->next->data = id2;
|
||||||
|
current->next->weight = weight;
|
||||||
|
current->next->next = NULL;
|
||||||
|
|
||||||
if (!flag) {
|
if (!flag) {
|
||||||
node *current2 = nodes.at(id2);
|
node *current2 = nodes.at(id2);
|
||||||
while(current2->next!=NULL) {
|
while (current2->next != NULL) {
|
||||||
current2 = current2->next;
|
current2 = current2->next;
|
||||||
}
|
}
|
||||||
current2->next=new node();
|
current2->next = new node();
|
||||||
current2->next->data=id1;
|
current2->next->data = id1;
|
||||||
current2->next->weight = weight;
|
current2->next->weight = weight;
|
||||||
current2->next->next=NULL;
|
current2->next->next = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string to_string() {
|
string to_string() {
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
for (int i=0;i<=nodes.size()-1;i++) {
|
for (int i = 0; i <= nodes.size() - 1; i++) {
|
||||||
node* current = nodes.at(i);
|
node *current = nodes.at(i);
|
||||||
while(current!=NULL) {
|
while (current != NULL) {
|
||||||
if(current->next==NULL) {
|
if (current->next == NULL) {
|
||||||
if (isWeighted) {
|
if (isWeighted) {
|
||||||
ss<<current->data<<"|"<<current->weight;
|
ss << current->data << "|" << current->weight;
|
||||||
|
} else {
|
||||||
|
ss << current->data;
|
||||||
}
|
}
|
||||||
else {
|
} else {
|
||||||
ss<<current->data;
|
if (isWeighted) {
|
||||||
}
|
ss << current->data << "|" << current->weight << " ";
|
||||||
}else {
|
} else {
|
||||||
if (isWeighted){
|
ss << current->data << " ";
|
||||||
ss<<current->data<<"|"<<current->weight<<" ";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
ss<<current->data<<" ";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
ss<<endl;
|
ss << endl;
|
||||||
}
|
}
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<T> BFS(int startId) {
|
vector<T> BFS(int startId) {
|
||||||
if (startId >= nodes.size() || startId < 0) {
|
if (startId >= nodes.size() || startId < 0) {
|
||||||
throw NodeIdOutOfRangeException("起始节点id超出范围", 0X004);
|
throw NodeIdOutOfRangeException("起始节点id超出范围", 0X004);
|
||||||
@@ -134,7 +143,7 @@ public:
|
|||||||
q.pop();
|
q.pop();
|
||||||
result.push_back(currentId);
|
result.push_back(currentId);
|
||||||
|
|
||||||
node* current = nodes.at(currentId);
|
node *current = nodes.at(currentId);
|
||||||
// 跳过id节点和值节点
|
// 跳过id节点和值节点
|
||||||
current = current->next->next;
|
current = current->next->next;
|
||||||
|
|
||||||
|
|||||||
+48
-13
@@ -6,30 +6,65 @@
|
|||||||
#define MATRIXGRAPH_H
|
#define MATRIXGRAPH_H
|
||||||
|
|
||||||
#include<vector>
|
#include<vector>
|
||||||
|
|
||||||
|
#include "GraphExceptions/InsertExistedConnectException.h"
|
||||||
|
#include "GraphExceptions/NodeIdOutOfRangeException.h"
|
||||||
|
#include "GraphExceptions/SameNodeConnectException.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class MatrixGraph {
|
class MatrixGraph {
|
||||||
private:
|
private:
|
||||||
vector<T> data;
|
struct connection {
|
||||||
vector<vector<bool> > matrix;
|
T weight; //权
|
||||||
int nodeCount;
|
bool connected; //是否连接
|
||||||
|
};
|
||||||
|
|
||||||
|
vector<T> data; //顶点值
|
||||||
|
vector<vector<connection> > matrix; //邻接矩阵
|
||||||
|
bool flag; //是否为有向图
|
||||||
|
bool isWeighted; //是否有权
|
||||||
public:
|
public:
|
||||||
MatrixGraph() {
|
MatrixGraph(bool flag = false, bool isWeighted = false) {
|
||||||
matrix.clear();
|
this->flag = flag;
|
||||||
|
this->isWeighted = isWeighted;
|
||||||
data.clear();
|
data.clear();
|
||||||
nodeCount = 0;
|
matrix.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 添加顶点
|
||||||
void insert(T value) {
|
void insert(T value) {
|
||||||
data.push_back(value);
|
data.push_back(value);
|
||||||
nodeCount++;
|
matrix.push_back(vector<connection>(0, false)); //添加一行
|
||||||
matrix.push_back(vector<bool>(nodeCount, false));
|
for (auto &row: matrix) {
|
||||||
for(int i = 0; i < nodeCount-1; ++i) {
|
row.resize(data.size(), connection(0, false)); // 扩展列
|
||||||
matrix[i].push_back(false);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接顶点
|
||||||
|
void connect(int id1, int id2, T weight = 0) {
|
||||||
|
if (id1 > data.size() || id2 > data.size() || (id1 < 0 || id2 < 0)) {
|
||||||
|
throw NodeIdOutOfRangeException("您提供的id不合理", 0X001);
|
||||||
|
}
|
||||||
|
if (id1 == id2) {
|
||||||
|
throw SameNodeConnectException("禁止连接两个相同节点", 0x002);
|
||||||
|
}
|
||||||
|
if (matrix[id1][id2].connected) {
|
||||||
|
if (matrix[id1][id2].weight == weight) {
|
||||||
|
throw InsertExistedConnectException("禁止插入已存在的连接", 0x003);
|
||||||
|
}
|
||||||
|
matrix[id1][id2].weight = weight;
|
||||||
|
if (flag == false) {
|
||||||
|
matrix[id2][id1].weight = weight;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
matrix[id1][id2].connected = true;
|
||||||
|
matrix[id1][id2].weight = weight;
|
||||||
|
if (flag == false) {
|
||||||
|
matrix[id2][id1].connected = true;
|
||||||
|
matrix[id2][id1].weight = weight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif //MATRIXGRAPH_H
|
#endif //MATRIXGRAPH_H
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
// 利用邻接矩阵存储无向图,并从0号顶点开始进行深度优先遍历。
|
||||||
|
// 输入:
|
||||||
|
// 输入第一行是两个整数n e,其中n表示顶点数(则顶点编号为0至n-1),e表示图中的边数。之后有e行信息输入,每行输入表示一条边,格式是“顶点1 顶点2”,把边插入图中。如:
|
||||||
|
// 4 4
|
||||||
|
// 0 1
|
||||||
|
// 1 3
|
||||||
|
// 0 3
|
||||||
|
// 0 2
|
||||||
|
// 输出:
|
||||||
|
// 先输出存储图的邻接矩阵,同一行元素之间空1格,最后一个元素之后不要有空格。
|
||||||
|
// 之后空一行后输出从0号顶点开始的深度优先遍历序列,顶点编号之间空1格。
|
||||||
|
// 例如,对于上面的示例输入,输出为:
|
||||||
|
// 0 1 1 1
|
||||||
|
// 1 0 0 1
|
||||||
|
// 1 0 0 0
|
||||||
|
// 1 1 0 0
|
||||||
|
// 从顶点0开始的深度优先遍历序列:
|
||||||
|
// 0 1 3 2
|
||||||
|
// 说明
|
||||||
|
// 输入第1个4表示有4个顶点,第2个4表示有4条边。之后的4行输入代表4条边的顶点。
|
||||||
|
// 输出的前4行为邻接矩阵,之后空一行,然后输出的“0 1 3 2”是深度优先遍历序列。
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include"MatrixGraph.h"
|
||||||
|
using namespace std;
|
||||||
|
int main() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user