Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- THE HPP FILE
- #ifndef __GRAPH_HPP
- #define __GRAPH_HPP
- #include <vector>
- class WeightedGraph {
- public:
- // Constructor. Know the graph size in advance.
- WeightedGraph(int num_nodes);
- // Destructor
- ~WeightedGraph();
- // Add a new edge to the graph
- // If it exists already, update the weight.
- void addEdge(int u, int v, double w);
- // Get the number of nodes in the graph
- int numNodes() const;
- // Get the number of edges in the graph
- int numEdges() const;
- // Return a list of neighbors for a given vertex
- std::vector<int> neighbors(int u);
- // Check if two nodes are connected
- bool areConnected(int u, int v);
- // Get the weight of the edge between two nodes.
- // If there is no edge, return 0.
- double getWeight(int u, int v);
- // print out the graph’s adjacency matrix
- void print();
- private:
- int capacity;
- vector<double> parts;
- };
- #endif // header include guard
- THE CPP FILE
- #include "graph.hpp"
- #include <iostream>
- #include <vector>
- using namespace std;
- WeightedGraph::WeightedGraph(int num_nodes) { //Cj
- int capacity = num_nodes;
- }
- WeightedGraph::~WeightedGraph() {
- delete parts;
- }
- void WeightedGraph::addedge(int u, int v, double w) { //Cj
- double x = static_cast<double>(u);
- double z = static_cast<double>(v);
- for (int i = 0; i <= (parts.size() - 2); i = i + 3) {
- if ((x == i) && (z == (i + 1))) {
- parts.at(i + 2) = w;
- }
- }
- parts.pusback(x);
- parts.pusback(z);
- parts.pusback(w);
- }
- int WeightedGraph::numNodes() const { //Cj
- return capacity;
- }
- int WeightedGraph::numEdges() const { //Cj
- return (parts.size() / 3);
- }
- vector<int> WeightedGraph::neighbors(int u) {
- for (int i = 0; i <= parts.size(); i = i + 3) {
- if ((i == u) && (((i + 1) % 3) != 0)) {
- //HOW DO I ADD TO THE VECTOR (USE ARECONNECTED, if so, add number to list)
- //remember that the numbers can be reversed
- }
- }
- //WHAT IS THE NAME TO RETURN IT //i have to create my own vector
- }
- bool WeightedGraph::areConnected(int u, int v) {
- for (int i = 0; i <= (parts.size() - 2); i = i + 3) { //Dan
- if ((u == i) && (v == (i + 1))) {
- return true;
- }
- if (i = (parts.size() - 2)) {
- return false;
- }
- }
- }
- double WeightedGraph::getWeight(int u, int v) {
- for (int i = 0; i <= (parts.size() - 2); i = i + 3) {
- if ((u == i) && (v == (i + 1))) {
- return parts.at(i = i + 2);
- }
- if (i = (parts.size() - 2)) {
- return 0;
- }
- }
- }
- void WeightedGraph::print() {
- int x = 0; //CHECK THIS
- int y = 0;
- for (int j = 1; j <= (capacity * capacity); j++) {
- cout << getWeight(x, y) << " "; //Does this work for reverse? How can i get this to work for reverse?
- y++;
- if ((j % capacity) != 0) {
- cout << endl;
- x++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement