Skip to content

Instantly share code, notes, and snippets.

@EmmaG2
Created September 7, 2023 14:33
Show Gist options
  • Save EmmaG2/11e84efcc5266c0aee74949721f7b260 to your computer and use it in GitHub Desktop.
Save EmmaG2/11e84efcc5266c0aee74949721f7b260 to your computer and use it in GitHub Desktop.
Esta es una implementación de grafos con lista de adyacencia en el lenguaje C++
#include <vector>
#include <iostream>
#define vii vector< vector<int> >
using namespace std;
void create_graph(int nodes, vii &graph)
{
for (int i = 0; i < nodes; i++)
{
vector <int> row(0, 0);
graph.push_back(row);
}
}
void add_relation(int node1, int node2, vii &graph)
{
graph[node1].push_back(node2);
graph[node2].push_back(node1);
}
void print_graph(vii &graph)
{
for (int i = 0; i < graph.size(); i++)
{
cout << "Nodo " << i << ": ";
for (int j = 0; j < graph[i].size(); j++)
{
cout << graph[i][j] << " ";
}
cout << endl;
}
}
int main()
{
vii grafo1;
create_graph(4, grafo1);
add_relation(0, 1);
add_relation(1, 2, grafo1);
add_relation(1, 3, grafo1);
print_graph(grafo1);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment