mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-15 11:20:05 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class graph
|
||||
{
|
||||
class graph {
|
||||
int v;
|
||||
list<int> *adj;
|
||||
|
||||
@@ -11,56 +10,46 @@ class graph
|
||||
void printgraph();
|
||||
void bfs(int s);
|
||||
};
|
||||
graph::graph(int v)
|
||||
{
|
||||
graph::graph(int v) {
|
||||
this->v = v;
|
||||
this->adj = new list<int>[v];
|
||||
}
|
||||
void graph::addedge(int src, int dest)
|
||||
{
|
||||
void graph::addedge(int src, int dest) {
|
||||
src--;
|
||||
dest--;
|
||||
adj[src].push_back(dest);
|
||||
// adj[dest].push_back(src);
|
||||
}
|
||||
void graph::printgraph()
|
||||
{
|
||||
for (int i = 0; i < this->v; i++)
|
||||
{
|
||||
void graph::printgraph() {
|
||||
for (int i = 0; i < this->v; i++) {
|
||||
cout << "Adjacency list of vertex " << i + 1 << " is \n";
|
||||
list<int>::iterator it;
|
||||
for (it = adj[i].begin(); it != adj[i].end(); ++it)
|
||||
{
|
||||
for (it = adj[i].begin(); it != adj[i].end(); ++it) {
|
||||
cout << *it + 1 << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
void graph::bfs(int s)
|
||||
{
|
||||
void graph::bfs(int s) {
|
||||
bool *visited = new bool[this->v + 1];
|
||||
memset(visited, false, sizeof(bool) * (this->v + 1));
|
||||
visited[s] = true;
|
||||
list<int> q;
|
||||
q.push_back(s);
|
||||
list<int>::iterator it;
|
||||
while (!q.empty())
|
||||
{
|
||||
while (!q.empty()) {
|
||||
int u = q.front();
|
||||
cout << u << " ";
|
||||
q.pop_front();
|
||||
for (it = adj[u].begin(); it != adj[u].end(); ++it)
|
||||
{
|
||||
if (visited[*it] == false)
|
||||
{
|
||||
for (it = adj[u].begin(); it != adj[u].end(); ++it) {
|
||||
if (visited[*it] == false) {
|
||||
visited[*it] = true;
|
||||
q.push_back(*it);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
graph g(4);
|
||||
g.addedge(1, 2);
|
||||
g.addedge(2, 3);
|
||||
|
||||
Reference in New Issue
Block a user