有权边API
用法提示:得到一条边edge的两个顶点,这样用:
int v=edge.either(); int w=edge.other(v);
c++实现
//Edge.h
#pragma once
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
#define out(data) cout<<data<<" "
#define hh printf_s("\n")
class Edge
{
public:
Edge();
Edge(int v,int w,double weight);
double weight();
int either();
int other(int v);
void show();
string toString();
int compareTo(Edge& that);
private:
int m_v;//顶点之一
int m_w;//另一个顶点
double m_weight;
//辅助输出字符串
string intToStr(int n);
string doubleToStr(double x);
};
void UnvalidatedInput(string info);
//方便优先队列比较类对象
bool operator<(Edge a, Edge b);
bool operator>(Edge a, Edge b);
#include "Edge.h"
Edge::Edge():m_v(0),m_w(0),m_weight(0){}
Edge::Edge(int v, int w, double weight):m_v(v),m_w(w),m_weight(weight){}
double Edge::weight()
{
return m_weight;
}
int Edge::either()
{
return m_v;
}
int Edge::other(int v)
{
if (m_v == v) return m_w;
else if (m_w == v) return m_v;
else {
UnvalidatedInput("no such point!.");
return -1;
}
}
int Edge::compareTo(Edge& that)
{
if (this->m_weight > that.weight()) return +1;
else if (this->m_weight < that.weight()) return -1;
else return 0;
}
void Edge::show()
{
printf_s("%d-%d %.2f", m_v, m_w, m_weight);
}
string Edge::toString()
{
string ans = "[ ";
ans += intToStr(m_v) + "-" + intToStr(m_w)
+ " " + doubleToStr(m_weight)+" ]";
return ans;
}
string
inline Edge::doubleToStr(double x) {
char double_str[100] = { 0 };
sprintf_s(double_str, "%.2f", x);
return string(double_str);
}
string
inline Edge::intToStr(int n)
{
char int_s[20] = { 0 };//末尾加上'\0'
_itoa_s(n, int_s, 10);// 10 表示十进制
return string(int_s);
}
void UnvalidatedInput(string info)
{
out(info), hh;
}
bool operator<(Edge a, Edge b)
{
return a.compareTo(b)<0;
}
bool operator>(Edge a, Edge b)
{
return a.compareTo(b)>0;
}
加权无向图
点这里Graph.h对比无权图,只是封装了边。
c++实现
//EdgeWeightGraph.h
#pragma once
#include<vector>
#include<list>
#include"Edge.h"
class EdgeWeightGraph
{
public:
EdgeWeightGraph(int V);
EdgeWeightGraph(string file);
int V() { return m_V; }
int E() { return m_E; }
void addEdge(Edge e);
list<Edge>* adj(int v);
list<Edge>* edges();
string toString();
private:
int m_V;
int m_E;
vector<list<Edge>*>* m_adj = nullptr;//邻接表
list<Edge>* m_edges = nullptr;//记录所有边
string intToStr(int n);
};
void testForEWG();
#include "EdgeWeightGraph.h"
EdgeWeightGraph::EdgeWeightGraph(int V):m_V(V),m_E(0)
{
m_adj = new vector<list<Edge>*>(V,nullptr);
for (int i = 0; i < V; ++i) {
m_adj->at(i) = new list<Edge>();
}
}
EdgeWeightGraph::EdgeWeightGraph(string file)
{
ifstream in(file, ios::in);
if (!in) {
out("open "), out(file),out(" error!"), hh;
return;
}
int V, E;
in >> V;
m_V = V;
m_adj = new vector<list<Edge>*>(V, nullptr);
for (int i = 0; i < V; ++i) {
m_adj->at(i) = new list<Edge>();
}
in >> E;
m_edges = new list<Edge>();
m_E = 0;
for (int i = 0; i < E; ++i) {
int v, w;
double wei=0.0;
in >> v >> w >> wei;
addEdge(Edge(v, w, wei));
}
in.close();
}
void EdgeWeightGraph::addEdge(Edge e)
{
int v = e.either(),w=e.other(v);
m_adj->at(v)->push_front(e);
m_adj->at(w)->push_front(e);
m_edges->push_front(e);
m_E++;
}
list<Edge>* EdgeWeightGraph::adj(int v)
{
return m_adj->at(v);//节点v的邻接链表
}
list<Edge>* EdgeWeightGraph::edges()
{
return m_edges;//返回所有边
}
string EdgeWeightGraph::toString()
{
string s = intToStr(m_V) + " vertices, " + intToStr(m_E) + " edges\n";
for (int i = 0; i < m_V; i++)
{
s += intToStr(i) + ": ";
for (auto adj : *(m_adj->at(i)))
{
s += adj.toString()+" ";
}
s += "\n";
}
return s;
}
string
inline EdgeWeightGraph::intToStr(int n)
{
char int_s[20] = { 0 };//末尾加上'\0'
_itoa_s(n, int_s, 10);// 10 表示十进制
return string(int_s);
}
void testForEWG()
{
EdgeWeightGraph ewG("tinyEWG.txt");
out(ewG.toString()),hh;
}
测试文件tinyEWG.txt
8
16
4 5 0.35
4 7 0.37
5 7 0.28
0 7 0.16
1 5 0.32
0 4 0.38
2 3 0.17
1 7 0.19
0 2 0.26
1 2 0.36
1 3 0.29
2 7 0.34
6 2 0.40
3 6 0.52
6 0 0.58
6 4 0.93