DS-Completed
This commit is contained in:
0
Algorithm/Divide&Conquer/分而治之
Normal file
0
Algorithm/Divide&Conquer/分而治之
Normal file
0
Algorithm/Greedy/贪心算法
Normal file
0
Algorithm/Greedy/贪心算法
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
140
Data-Structure/Graph/adjacencyList/graph.h
Normal file
140
Data-Structure/Graph/adjacencyList/graph.h
Normal file
@@ -0,0 +1,140 @@
|
||||
#pragma once
|
||||
/*
|
||||
<EFBFBD><EFBFBD><EFBFBD>ɴ<EFBFBD>0<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>Ҫ + 1
|
||||
*/
|
||||
//#include <limits>
|
||||
#include <deque>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
template <class T>
|
||||
struct edge {
|
||||
int from, to;
|
||||
T weight;
|
||||
|
||||
edge(int _from, int _to, const T& _weight) : from(_from), to(_to), weight(_weight) {}
|
||||
};
|
||||
template <class T>
|
||||
class adjacentList {
|
||||
private:
|
||||
int v;
|
||||
int e;
|
||||
std::deque<std::deque<std::pair<int, T>>> adj;//pair<to, weight>
|
||||
//<2F>ڽӱ<DABD>û<EFBFBD><C3BB>Ҫ<EFBFBD><D2AA>-static constexpr T INF = std::limits<T>::infinity();
|
||||
public:
|
||||
std::deque<bool> reach;
|
||||
|
||||
adjacentList() = delete;
|
||||
adjacentList(int _v) : v(_v), e(0) {
|
||||
adj.resize(v);
|
||||
reach.assign(v, false);
|
||||
}
|
||||
~adjacentList() = default;
|
||||
|
||||
bool validateV(int _v) const {
|
||||
return _v >= 0 && _v < v;
|
||||
}
|
||||
int numberOfVertices() const { return v; }
|
||||
int numberOfEdges() const { return e; }
|
||||
bool existsEdge(int _a, int _b) const;
|
||||
void insertEdge(edge<T>* _e);
|
||||
void eraseEdge(int _a, int _b);
|
||||
int degree(int _v) const { return inDegree(_v) + outDegree(_v); }
|
||||
int inDegree(int _v) const;
|
||||
int outDegree(int _v) const;
|
||||
void bfs(int _v);
|
||||
void dfs(int _v);
|
||||
};
|
||||
|
||||
template<class T>
|
||||
inline bool adjacentList<T>::existsEdge(int _a, int _b) const
|
||||
{
|
||||
if (!validateV(_a) || !validateV(_b)) return false;
|
||||
for (auto x : adj[_a]) {
|
||||
if (x.first == _b) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void adjacentList<T>::insertEdge(edge<T>* _e)
|
||||
{
|
||||
if (!validateV(_e->from) || !validateV(_e->to)) return;
|
||||
adj[_e->from].push_back(std::make_pair(_e->to, _e->weight));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void adjacentList<T>::eraseEdge(int _a, int _b)
|
||||
{
|
||||
if (!validateV(_a) || !validateV(_b)) return;
|
||||
for (auto it = adj[_a].begin(); it != adj[_a].end();) {
|
||||
if ((*it).first == _b) {
|
||||
it = adj[_a].erase(it);//erase<73>᷵<EFBFBD><E1B7B5><EFBFBD>µ<EFBFBD>it<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline int adjacentList<T>::inDegree(int _v) const
|
||||
{
|
||||
if (!validateV(_v)) return 0;
|
||||
int js = 0;
|
||||
for (int i = 0; i < v; i++) {
|
||||
/*for (auto x : adj[i]) {
|
||||
if (x.first == _v) js++;
|
||||
}*/
|
||||
//<2F><>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>: pair - [to, w]
|
||||
for (auto [to, w] : adj[i]) {
|
||||
if (to == _v) js++;
|
||||
}
|
||||
}
|
||||
return js;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline int adjacentList<T>::outDegree(int _v) const
|
||||
{
|
||||
if (!validateV(_v)) return 0;
|
||||
return adj[_v].size();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void adjacentList<T>::bfs(int _v)
|
||||
{
|
||||
if (!validateV(_v)) return;
|
||||
std::deque<int> que;
|
||||
std::deque<bool> visited(v, false);
|
||||
|
||||
que.push_back(_v);
|
||||
visited[_v] = true;
|
||||
|
||||
while (!que.empty()) {
|
||||
int u = que.front();
|
||||
que.pop_front();
|
||||
std::cout << u << " ";
|
||||
for (auto [to, w] : adj[u]) {
|
||||
if (visited[to] == false) {
|
||||
que.push_back(to);
|
||||
visited[to] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void adjacentList<T>::dfs(int _v)
|
||||
{
|
||||
if (!validateV(_v)) return;
|
||||
reach[_v] = true;
|
||||
std::cout << _v << " ";
|
||||
for (auto [to, w] : adj[_v]) {
|
||||
if (!reach[to]) {
|
||||
//reach[to] = true;
|
||||
dfs(to);
|
||||
}
|
||||
}
|
||||
}
|
22
Data-Structure/Graph/adjacencyList/main.cpp
Normal file
22
Data-Structure/Graph/adjacencyList/main.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "graph.h"
|
||||
using namespace std;
|
||||
int main() {
|
||||
adjacentList<double> g(11);
|
||||
edge<double> e1(1, 2, 0.5), e2(1, 3, 0.6), e3(1, 4, 0.7), e4(1, 5, 0.8), e5(3, 10, 0.9);
|
||||
edge<double> e6(10, 7, 0.5), e7(2, 5, 0.6), e8(5, 8, 0.7);
|
||||
g.insertEdge(&e1);g.insertEdge(&e2);g.insertEdge(&e3);g.insertEdge(&e4);g.insertEdge(&e5);g.insertEdge(&e6);g.insertEdge(&e7);g.insertEdge(&e8);
|
||||
cout << g.degree(1) << endl;
|
||||
cout << g.inDegree(5) << endl;
|
||||
cout << g.outDegree(8) << endl;
|
||||
g.bfs(1);
|
||||
g.dfs(1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
4
|
||||
2
|
||||
0
|
||||
1 2 3 4 5 10 8 7
|
||||
1 2 5 8 3 10 7 4
|
||||
*/
|
0
Data-Structure/结束于2025-8-29
Normal file
0
Data-Structure/结束于2025-8-29
Normal file
Reference in New Issue
Block a user