Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<set>
- #include<list>
- #include<limits>
- #include<algorithm>
- using namespace std;
- struct Node {
- int dest;
- int cost;
- };
- class Graph {
- int n;
- list<Node>* adjList;
- private:
- void showList(int src, list<Node> lt) {
- Node tempNode;
- for (list<Node>::iterator i = lt.begin(); i != lt.end(); i++) {
- tempNode = *i;
- cout << "(" << src << ")---(" << tempNode.dest << "|" << tempNode.cost << ") ";
- }
- cout << endl;
- }
- public:
- Graph() {
- n = 0;
- }
- Graph(int nodeCount) {
- n = nodeCount;
- adjList = new list<Node>[n];
- }
- void addEdge(int source, int dest, int cost) {
- Node newNode;
- newNode.dest = dest;
- newNode.cost = cost;
- adjList[source].push_back(newNode);
- }
- void displayEdges() {
- for (int i = 0; i < n; i++) {
- list<Node> tempList = adjList[i];
- showList(i, tempList);
- }
- }
- static int findDegree(Graph G, int ver)
- {
- return G.adjList[ver].size();
- }
- friend void dijkstra(Graph g, int* dist, int* prev, int start);
- };
- void dijkstra(Graph g, int* dist, int* prev, int start) {
- int n = g.n;
- for (int u = 0; u < n; u++) {
- dist[u] = numeric_limits<int>::max();
- prev[u] = -1;
- }
- dist[start] = 0;
- set<int> S;
- list<int> Q;
- for (int u = 0; u < n; u++) {
- Q.push_back(u);
- }
- while (!Q.empty()) {
- list<int> ::iterator i;
- i = min_element(Q.begin(), Q.end());
- int u = *i;
- Q.remove(u);
- S.insert(u);
- for (list<Node>::iterator it = g.adjList[u].begin(); it != g.adjList[u].end(); it++) {
- if ((dist[u] + (it->cost)) < dist[it->dest]) {
- dist[it->dest] = (dist[u] + (it->cost));
- prev[it->dest] = u;
- }
- }
- }
- }
- int main() {
- const int n = 7;
- Graph g(n);
- int dist[n], prev[n];
- int start = 0;
- g.addEdge(0, 1, 3);
- g.addEdge(0, 2, 6);
- g.addEdge(1, 0, 3);
- g.addEdge(1, 2, 2);
- g.addEdge(1, 3, 1);
- g.addEdge(2, 1, 6);
- g.addEdge(2, 1, 2);
- g.addEdge(2, 3, 1);
- g.addEdge(2, 4, 4);
- g.addEdge(2, 5, 2);
- g.addEdge(3, 1, 1);
- g.addEdge(3, 2, 1);
- g.addEdge(3, 4, 2);
- g.addEdge(3, 6, 4);
- g.addEdge(4, 2, 4);
- g.addEdge(4, 3, 2);
- g.addEdge(4, 5, 2);
- g.addEdge(4, 6, 1);
- g.addEdge(5, 2, 2);
- g.addEdge(5, 4, 2);
- g.addEdge(5, 6, 1);
- g.addEdge(6, 3, 4);
- g.addEdge(6, 4, 1);
- g.addEdge(6, 5, 1);
- dijkstra(g, dist, prev, start);
- for (int i = 0; i < n; i++)
- if (i != start)
- cout << start << " to " << i << ", Cost: " << dist[i] << " Previous: " << prev[i] << endl;
- cout << "VER: " << Graph::findDegree(g, 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement