-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafos.cpp
104 lines (95 loc) · 2.58 KB
/
grafos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Graph implementation following tutorial http://www.geeksforgeeks.org/graph-and-its-representations/
*/
#include<iostream>
#include<cstdlib>
using namespace std;
//struct for an adjacency list node
struct AdjListNode{
int data;
int costo;
AdjListNode *next;
};
//struct for an adjacency list
struct AdjList{
AdjListNode *head; //pointer to head node of list
};
//struct for a graph. A graph as an array of adjacency lists
//Size of array will be V (total vertices)
struct Graph{
int V;
AdjList *arr;
};
AdjListNode *newAdjListNode(int,int);
Graph *createGraph(int);
void addEdge(Graph*,int,int,int);
void printGraph(Graph*);
int main(){
//create a new graph
int totalVertices=4;
Graph *graph;
graph=createGraph(totalVertices);
//connect edges
addEdge(graph,0,1,20);
addEdge(graph,0,2,10);
addEdge(graph,0,3,5);
addEdge(graph,1,3,15);
addEdge(graph,2,3,6);
/*
addEdge(graph,0,1);
addEdge(graph,0,4);
addEdge(graph,1,2);
addEdge(graph,1,3);
addEdge(graph,1,4);
addEdge(graph,2,3);
addEdge(graph,3,4);
*/
//print the adjacency list representation of graph
printGraph(graph);
}
//create a new node
AdjListNode* newAdjListNode(int data, int costo){
AdjListNode *nptr=new AdjListNode;
nptr->data=data;
nptr->costo=costo;
nptr->next=NULL;
return nptr;
}
//function to create a graph of V - vertices
Graph* createGraph(int V){
Graph *graph=new Graph;
graph->V=V;
//create an array of adjacency list. size of array - V
graph->arr=new AdjList[V];
//initialize with NULL (e.g root=NULL)
for(int i=0;i<V;i++){
graph->arr[i].head=NULL;
}
return graph;
}
//add an edge to an undirected Graph
void addEdge(Graph *graph,int src,int dest, int costo){
//Add an edge from src to dest. A new node added to the adjacency list of src
//node added at beginning
AdjListNode *nptr=newAdjListNode(dest, costo);
nptr->next=graph->arr[src].head;
graph->arr[src].head=nptr;
//connect from dest to src (since undirected)
nptr=newAdjListNode(src, costo);
nptr->next=graph->arr[dest].head;
graph->arr[dest].head=nptr;
}
//function to print the graph
void printGraph(Graph* graph){
//loop over each adjacent list
for(int i=0;i<graph->V;i++){
AdjListNode *root=graph->arr[i].head;
cout<<"Rutas del vertice "<<i<<endl;
//loop over each node in list
while(root!=NULL){
cout << i <<" -> "<< root->data << " Costo: "<< root->costo <<endl;
root=root->next;
}
cout<<endl;
}
}