diff --git a/directed_graph.cpp b/directed_graph.cpp index c4aa70b..e2b8751 100644 --- a/directed_graph.cpp +++ b/directed_graph.cpp @@ -635,7 +635,7 @@ int _connected_vertices_count_recursive(DirectedGraph* graph, void* origin) /* * The source_vertex function takes a DirectedGraph* struct pointer parameter and - * traverses the Directed Graph structure from each vertex. The vertex from which + * traverses the Directed Graph structure from each vertex. The first vertex from which * all vertices can be visited is returned. If no such vertex exists, then NULL * is returned. */ @@ -696,7 +696,7 @@ void set_visited_field(DirectedGraph* graph, bool value) } /* - * The reset_parent_links function changes the parent reference of eac vertex struct + * The reset_parent_links function changes the parent reference of each vertex struct * within the DirectedGraph* struct to NULL. */ /* @@ -722,6 +722,7 @@ void reset_parent_links(DirectedGraph* graph) * accepts two vertex* structs as void*, casts them to Vertex*, * and compares them based on their distance data member. */ +/* int compareVertex(void* a, void* b) { if(a == NULL || b == NULL) @@ -733,7 +734,7 @@ int compareVertex(void* a, void* b) return (int) v1->distance - v2->distance; } - +*/ /* * This buildTree funciton implements dijkstra's algorithm. It takes a DirectedGraph* struct * pointer and a void* origin vertex struct pointer, then creates a shotest path list by diff --git a/directed_graph.h b/directed_graph.h index 632020c..c367d12 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -1,10 +1,9 @@ /* - * This is a direcected graph header file - providing the forward declarations - * for the directed_graph.c file. + * This file provides the declaration and implementation of the + digraph template class. */ -#ifndef __DIRECTED_GRAPH_HEADER -#define __DIRECTED_GRAPH_HEADER +#pragma once #include #include @@ -12,46 +11,352 @@ #include #include #include "linked_list.h" -#include "vertex.h" +//#include "vertex.h" +#include "vertex0.h" #include "priority_queue.h" #define MAX FLT_MAX -typedef struct _DirectedGraph -{ - //LinkedList* vertexList; - int valueSize; - float** adjacencyMatrix; - -} DirectedGraph; - -DirectedGraph* initialize_digraph(int, char*); -int digraph_size(DirectedGraph*); -Vertex* get_vertex(DirectedGraph*, void*); -bool add_vertex(DirectedGraph*, void*); -bool remove_vertex(DirectedGraph*, void*); -//LinkedList* get_vertices(DirectedGraph*); -//LinkedList* get_arcs(DirectedGraph*, void*); -bool add_arc(DirectedGraph*, void*, void*, float); -bool remove_arc(DirectedGraph*, void*, void*); -bool change_arc_weight(DirectedGraph*, void*, void*, float); -int connected_vertices_count(DirectedGraph*, void*); -int _connected_vertices_count_recursive(DirectedGraph*, void*); -void* source_vertex(DirectedGraph*); -void set_visited_field(DirectedGraph*, bool); -void buildTree(DirectedGraph*, void*); -float get_arc_weight(DirectedGraph*, void*, void*); -void reset_parent_links(DirectedGraph*); -int compareVertex(void*, void*); -//LinkedList* dijkstra(DirectedGraph*, void*, void*); -float** all_pairs_shortest_paths(DirectedGraph*); -void create_adjacency_matrix(DirectedGraph*); -float** get_adjacency_matrix(DirectedGraph*); -bool contains_vertex(DirectedGraph*, void*); -DirectedGraph* create_digraph_from_file(char*); -float extract_value(int, int, char*); -float* float_arr_from_str(char*); -int value_count(char*); -std::ostream& operator<<(std::ostream& o, DirectedGraph& net); - -#endif +template class DirectedGraph { + public: + DirectedGraph(); // Tested + int getSize(); // Tested + bool addVertex(T); // Tested + bool removeVertex(T); // Tested + LinkedList* get_arcs(DirectedGraph*, void*); + bool addArc(T, T, K); // Tested + bool removeArc(T, T); // Tested + bool changeArcWeight(T, T, K); // Tested + int connectedVerticesCount(T); // Tested + Vertex* sourceVertex(); // Tested + void setVisitedField(bool); // Tested + void buildTree(Vertex*); + K getArcWeight(T, T); // Tested + int compareVertex(void*, void*); // Maybe require the user to ensure that T or K (which one?) implements the "comparable interface". I need to develop this interface. + LinkedList* dijkstra(DirectedGraph*, void*, void*); + float** all_pairs_shortest_paths(DirectedGraph*); + void create_adjacency_matrix(DirectedGraph*); + float** get_adjacency_matrix(DirectedGraph*); + bool contains_vertex(DirectedGraph*, void*); + DirectedGraph* create_digraph_from_file(char*); + float extract_value(int, int, char*); + float* float_arr_from_str(char*); + int valueCount(char*); // Implemented + //std::ostream& operator<<(std::ostream& o, DirectedGraph& net); + void enumerateVertices(); + + private: + LinkedList*>* vertexList; + int valueSize; + float** adjacencyMatrix; + Vertex* getVertex(T); // Tested + LinkedList*>* getVertices(); // Tested + int connectedVerticesCountHelper(Vertex*); // Tested + void resetParentLinks(); // Implemented + +}; + +/* + * Constructs a Directed Graph object. + */ +template DirectedGraph::DirectedGraph(){ + this->vertexList = new LinkedList*>(); + this->adjacencyMatrix = nullptr; +} + +/* + * Returns the number of vertices in the Directed Graph. + */ +template int DirectedGraph::getSize(){ + return this->vertexList->getSize(); +} + +/* + * Searches the Directed Graph for a vertex that contains the value parameter. + * A pointer to that vertex is returned if found. + */ +template Vertex* DirectedGraph::getVertex(T value){ + + int size = this->vertexList->getSize(); + + // For each vertex in the vertex list. + for(int i = 0; i < size; i++) { + + // Retrieve the vertex at index i in the vertex list. + Vertex* v = this->vertexList->get(i); + + // If the data fieled variable in the vertex is equal to the element parameter. + if(v->getData() == value) { + // Return this vertex pointer. + return v; + } + } + + return nullptr; +} + +/* + * The add vertex function creates a Vertex* pointer and adds it to the Directed Graph struct. + * True is returned if the vertex was successfully added to the Directed Graph. Otherwie false + * is returned. + */ +template bool DirectedGraph::addVertex(T element){ + + // Instantiate a Vertex struct while passing in the element parameter. + Vertex* v = new Vertex(element); + + // Return the call to linkedlist add last function while passing in + // Directed Graph's vertex list and the instantiated vertex. + return this->vertexList->addLast(v); +} + +/* + * The remove function removes a specific Vertex containing the + element parameter from the Directed Graph, if found. + */ +template bool DirectedGraph::removeVertex(T element){ + + // Store vertex list's current size in a local variable. + int prevSize = this->vertexList->getSize(); + bool found = false; + Vertex* vtx; + // For each vertex in the vertex list. + for(int i = 0; i < prevSize; i++) { + + // Retrieve the vertex in the vertex list at index i. + Vertex* v = this->vertexList->get(i); + + // If this vertex is equivalent to the element parameter. + if(v->getData() == element) { + // Remove this vertex from the vertex list. + vtx = this->getVertex(element); + this->vertexList->remove(i); + found = true; + break; + } + } + + if(found){ + for(int i = 0; i < this->vertexList->getSize(); i++){ + Vertex* v = this->vertexList->get(i); + v->removeArc(vtx); + } + return true; + } else { + return false; + } +} + +/* + * Returns the list of vertices in the Directed Graph. + */ +template LinkedList*>* DirectedGraph::getVertices(){ + return this->vertexList; +} + +/* + * The add arc function takes a DirectedGraph* struct pointer, a void* pointer to + * the origin vertex, a void* pointer to the destination vertex, and a float value + * representing the weight of the arc. The function then creates an arc from the + * origin vertex to the destination vertex with the appropriate weight assigned. + * This function returns true if the arc between origin and destination was added + * to the Directed Graph struct successfully, otherwise false is returned. + */ +template bool DirectedGraph::addArc(T origin, T destination, K cost) { + + // Retrieve the Vertices associated with the origin and destination parameters. + Vertex* start = getVertex(origin); + Vertex* end = getVertex(destination); + + // Return the call to add vertex arc function with the two vertices and the arc weight. + return start->addArc(end, cost); +} + + +/* + * The remove arc function removes the arc from the origin veretx to the destination vertex + * parameters. + */ +template bool DirectedGraph::removeArc(T origin, T destination) { + Vertex* start = getVertex(origin); + Vertex* end = getVertex(destination); + return start->removeArc(end); +} + +/* + * The set_visited_field function changes the visited data member for all + * vertex structs within the DirectedGraph* struct to the value of the bool parameter. + */ +template void DirectedGraph::setVisitedField(bool value) { + + // For each vertex in the Directed Graph's vertex list. + for(int i = 0; i < this->vertexList->getSize(); i++) { + Vertex* v = this->vertexList->get(i); + v->setVisited(value); + } +} + +template void DirectedGraph::enumerateVertices() { + + for(int i = 0; i < this->vertexList->getSize(); i++) { + + Vertex* v = this->vertexList->get(i); + std::map*, K> map = v->getArcMap(); + std::cout << v->getData() << ":\n"; + + for(auto j = map.begin(); j != map.end(); j++){ + std::cout << "\t" << j->first->getData() << ": " << j->second << "\n"; + } + } +} + +template bool DirectedGraph:: changeArcWeight(T origin, T destination, K weight){ + Vertex* start = this->getVertex(origin); + Vertex* end = this->getVertex(destination); + return start->changeWeight(end, weight); +} + +template K DirectedGraph::getArcWeight(T origin, T destination){ + Vertex* start = this->getVertex(origin); + Vertex* end = this->getVertex(destination); + return start->getWeight(end); +} + +/* + * The sourceVertex function traverses the Directed Graph structure from each + * vertex. The first vertex found from which all vertices can be visited is returned. + * If no such vertex exists, then NULL is returned. + */ +template Vertex* DirectedGraph::sourceVertex(){ + int numOfVertices = this->vertexList->getSize(); + for(int i = 0; i < numOfVertices; i++){ + Vertex* v = this->vertexList->get(i); + int visited = this->connectedVerticesCountHelper(v); + if(visited == numOfVertices){ + return v; + } + } + + return nullptr; +} + +/* + * The connectedVerticesCount function takes the data in the graph as a parameter, + * the origin to explore the graph from, calls a helper recursive function, resets + * all the vertices visited field to false, and returns the count of the vertices + * traversable from the origin parameter. + */ +template int DirectedGraph::connectedVerticesCount(T origin){ + Vertex* source = this->getVertex(origin); + int count = 0; + count += this->connectedVerticesCountHelper(source); + + this->setVisitedField(false); + return count; +} + +/* + * The connectedVerticesCountHelper function takesVertex pointer to serve as an origin vertex. + * The function then recursively visits each vertex traversable via the vertx's arcMap that + * has not been visited and counts the number of vertices. The count of the number of vertices + * visited is returned to the client. + */ +template int DirectedGraph::connectedVerticesCountHelper(Vertex* source){ + int count = 0; + + if(!source->getVisited()){ + source->setVisited(true); + count++; + + for(auto const& it : source->getArcMap()){ + count += connectedVerticesCountHelper(it.first); + } + + } else { + return count; + } + + return count; +} + +/* + * The resetParentLinks function changes the parent reference of each vertex in the graph's + * vertexList to nullptr. + */ +template void DirectedGraph::resetParentLinks(){ + + // For each vertex in the Directed Graph's vertex list. + for(int i = 0; i < this->vertexList->getSize(); i++){ + // Retrieve a reference to the vertex struct at index i. + Vertex* v = this->vertexList->get(i); + // Assign the vertex's parent reference to NULL. + v->setParent(nullptr); + } +} + +/** + * The valueCount function takes a char* parameter containing a csv of values and counts + * the number of values in the parameter string. This is equivalent to the number of commas + * plus one. + */ +template int DirectedGraph::valueCount(char* buffer){ + int commas = 0; + int size = strlen(buffer); + + for(int i = 0; i < size; i++){ if(buffer[i] == ','){ commas++;}} + commas++; + + return commas; +} + +/* + * This buildTree function implements dijkstra's algorithm. It takes a vertex pointer origin vertex + * then creates a shotest path list by assigning the parent data members for each vertex. + */ +template void DirectedGraph::buildTree(Vertex* root){ + if(root == nullptr) { return;} + + // For each vertex in the vertex list. + for(int i = 0; i < this->vertexList->getSize(); i++){ + // get the vertex at the index and assign its distance data field to MAX. + Vertex* v = this->vertexList->get(i); + v->setDistance(INT32_MAX); + } + + PriorityQueue* pq = new PriorityQueue(); + root->setDistance(0); + pq->enqueue(root, 0); + + // While the priority queue contains elements. + while(pq->getSize() > 0){ + // Dequeue a vertex from the priority queue. + Vertex* v = pq->dequeue(); + + // If this vertex has already been visited, force a new iteration. + if(v->getVisited){ continue; } + + // Set the vertex's visited data member to true. + v->setVisited(true); + + // Retrieve the list of arcs from this vertex. + std::map*, K> map = v->getArcMap(); + + // For each arc from this vertex. + for(auto j = map.begin(); j != map.end(); j++){ + float distance = v->getDistance() + j->second; + + // If the vertex's diatance is greater than the calculated distance. + if(j->first->getDistance() > distance){ + /* + * Assign the calculated distance to the current vertex, + * assign the parent value for this vertex and the vertex + * being dequeued, and enqueue this vertex. + */ + + j->first->setDistance(distance); + j->first->setParent(v); + pq->enqueue(j->first); + } + } + } +} \ No newline at end of file diff --git a/linked_list.h b/linked_list.h index 51efbea..0d53f82 100644 --- a/linked_list.h +++ b/linked_list.h @@ -24,14 +24,14 @@ template class LinkedList { public: LinkedList(); - bool add_at(int index, T element); - bool add_first(T element); - bool add_last(T element); - int index_of(T element); + bool addAt(int index, T element); + bool addFirst(T element); + bool addLast(T element); + int indexOf(T element); T get(int index); T remove(int index); - T remove_first(); - T remove_last(); + T removeFirst(); + T removeLast(); void swap(int index1, int index2); T getFirst(); T getLast(); @@ -50,13 +50,13 @@ template LinkedList::LinkedList() { this->size = 0; } -template bool LinkedList::add_at(int index, T element) { +template bool LinkedList::addAt(int index, T element) { if(index < 0) return false; if(index > this->size) - return add_last(element); + return addLast(element); Node *node = new Node(); node->data = element; @@ -97,12 +97,12 @@ template bool LinkedList::add_at(int index, T element) { return true; } -template bool LinkedList::add_first(T element) { - return add_at(0,element); +template bool LinkedList::addFirst(T element) { + return addAt(0,element); } -template bool LinkedList::add_last(T element) { - return add_at(this->size, element); +template bool LinkedList::addLast(T element) { + return addAt(this->size, element); } template T LinkedList::get(int index) { @@ -127,7 +127,7 @@ template T LinkedList::get(int index) { * Returns the index of the first occurrence of the element parameter. If the element * is not found, or if the element is a nullptr, then -1 is returned. */ -template int LinkedList::index_of(T element) { +template int LinkedList::indexOf(T element) { Node* temp = this->first; @@ -203,14 +203,14 @@ template T LinkedList::remove(int index) { /* * The linked_list_remove_first function removes the first element of the Linkedlist struct. */ -template T LinkedList::remove_first() { +template T LinkedList::removeFirst() { return this->remove(0); } /* * The remove_last function removes the last element of the Linkedlist struct. */ -template T LinkedList::remove_last() { +template T LinkedList::removeLast() { return this->remove(this->size - 1); } @@ -232,13 +232,13 @@ template< typename T> void LinkedList::swap(int index1, int index2) { if(index1 > index2){ this->remove(index1); this->remove(index2); - this->add_at(index2, temp1); - this->add_at(index1, temp2); + this->addAt(index2, temp1); + this->addAt(index1, temp2); } else { this->remove(index2); this->remove(index1); - this->add_at(index1, temp2); - this->add_at(index2, temp1); + this->addAt(index1, temp2); + this->addAt(index2, temp1); } } diff --git a/main.cpp b/main.cpp index 9d0dcd9..15884b1 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include #include "linked_list.h" #include "priority_queue.h" -//#include "directed_graph.h" +#include "directed_graph.h" #include #include #include @@ -25,16 +25,16 @@ class Animal{ int main(int argc, char** argv) { - +/* LinkedList AList = LinkedList(); Animal* a = new Animal("Fluffy"); Animal* b = new Animal("Scruffy"); Animal* c = new Animal("Moe"); - AList.add_last(a); - AList.add_first(b); - AList.add_at(1,c); + AList.addLast(a); + AList.addFirst(b); + AList.addAt(1,c); for (int i = 0; i < AList.getSize(); ++i) { Animal* x = AList.get(i); @@ -42,8 +42,8 @@ int main(int argc, char** argv) } Animal* d = AList.remove(1); - Animal* e = AList.remove_first(); - Animal* f = AList.remove_last(); + Animal* e = AList.removeFirst(); + Animal* f = AList.removeLast(); std::cout << "First: " << e->getName() << "\n"; std::cout << "Middle: " << d->getName() << "\n"; @@ -53,9 +53,9 @@ int main(int argc, char** argv) // delete e; // delete f; - AList.add_last(a); - AList.add_first(b); - AList.add_at(1,c); + AList.addLast(a); + AList.addFirst(b); + AList.addAt(1,c); Animal* g = AList.getFirst(); Animal* h = AList.getLast(); @@ -66,9 +66,9 @@ int main(int argc, char** argv) // delete g; // delete h; - int aIdx = AList.index_of(a); - int bIdx = AList.index_of(b); - int cIdx = AList.index_of(c); + int aIdx = AList.indexOf(a); + int bIdx = AList.indexOf(b); + int cIdx = AList.indexOf(c); // std::cout << "Index of Fluffy (expected 2): " << aIdx << "\n"; // std::cout << "Index of Moe (expected 1): " << cIdx << "\n"; @@ -76,13 +76,13 @@ int main(int argc, char** argv) std::cout << "swapping 0 and 2\n"; AList.swap(0,2); - std::cout << "Index of Fluffy (expected 0): " << AList.index_of(a)<< "\n"; - std::cout << "Index of Scruffy (expected 2): " << AList.index_of(b) << "\n"; + std::cout << "Index of Fluffy (expected 0): " << AList.indexOf(a)<< "\n"; + std::cout << "Index of Scruffy (expected 2): " << AList.indexOf(b) << "\n"; std::cout << "swapping 0 and 2 ... again\n"; AList.swap(2,0); - std::cout << "Index of Fluffy (expected 2): " << AList.index_of(a)<< "\n"; - std::cout << "Index of Scruffy (expected 0): " << AList.index_of(b) << "\n"; + std::cout << "Index of Fluffy (expected 2): " << AList.indexOf(a)<< "\n"; + std::cout << "Index of Scruffy (expected 0): " << AList.indexOf(b) << "\n"; printf("\nPriority Queue Testing\n\n"); @@ -123,61 +123,89 @@ int main(int argc, char** argv) std::cout << "Dequeue (expected Jim) " << pq->dequeue()->getName()<< "\n"; std::cout << "Dequeue (expected Bob) "<< pq->dequeue()->getName()<< "\n"; std::cout << "Dequeue (expected Moe) " << pq->dequeue()->getName()<< "\n"; - std::cout << "Dequeue (expected(Beth) " << pq->dequeue()->getName()<< "\n"; + std::cout << "Dequeue (expected Beth) " << pq->dequeue()->getName()<< "\n"; std::cout << "Dequeue (expected Becky) " << pq->dequeue()->getName()<< "\n"; std::cout << "Dequeue (expected Scruffy) " << pq->dequeue()->getName()<< "\n"; std::cout << "Dequeue (expected Fluffy) " << pq->dequeue()->getName()<< "\n"; std::cout << "list size (expected 0): " << pq->getSize() << "\n"; +*/ + printf("\t.....DiGraph Testing (constructor).....\n"); -/* - LinkedList* list = new LinkedList(); + DirectedGraph* graph = new DirectedGraph(); - list->add_first(0); - for(int i = 1; i < 99; i++){ - list->add_at(i,i); - } - list->add_last(99); + printf("....Adding Vertices (addVertex)....\n"); - int size = list->getSize(); + graph->addVertex('a'); + graph->addVertex('b'); + graph->addVertex('c'); + graph->addVertex('d'); + graph->addVertex('e'); + graph->addVertex('f'); - for (int i = 0; i < size; ++i) { - int item = list->get(i); - printf("%d\n", item); - } + // Creating Edges between vertices and assigning weights - int first = list->remove_first(); - int last = list->remove_last(); - - printf("first %d\n", first); - printf("last %d\n", last); + printf("....Adding Arcs (addArc)....\n"); + graph->addArc('a','b',1); + graph->addArc('a','c',2); + graph->addArc('a','d',7); + graph->addArc('a','f',2); + graph->addArc('b','d',17); + graph->addArc('b','f',8); + graph->addArc('c','d',1); + graph->addArc('e','a',6); + graph->addArc('f','d',0); - for (int i = list->getSize()-1; i >=0; i--){ - int element = list->remove(i); - printf("%d\n", element); - } -*/ + printf("\t.....Enumerating Connections in DiGraph.....\n"); + graph->enumerateVertices(); + + + printf("\t.....Removing Arcs (removeArc) from a to b and from f to d.....\n"); + graph->removeArc('a','b'); + graph->removeArc('f','d'); + graph->enumerateVertices(); + + printf("....Removing Vertex c and f (removeVertex)....\n"); + + char c = 'c'; + char f = 'f'; + graph->removeVertex(c); + graph->removeVertex(f); + graph->enumerateVertices(); + + int sz = graph->getSize(); + printf("....Calling getSize on graph: expecting 4 | actual %d....\n", sz); + + printf("Changing Arc Weight (changeArcWeight) from e to a from 6 to 264....\n"); + graph->changeArcWeight('e','a',264); + graph->enumerateVertices(); + + printf(".... Getting Arc Weight (getArcWeight) of e to a....\n"); + int weightEtoA = graph->getArcWeight('e','a'); + printf(".... Getting Arc Weight (getArcWeight) of e to b....\n"); + int weightEtoB = graph->getArcWeight('e','b'); + printf("Weight E to A (expected 264): %d | Weight E to B (expected -1): %d\n",weightEtoA, weightEtoB); + + printf("\n....Testing connected vertices count from e....\n"); + int fromE = graph->connectedVerticesCount('e'); + printf("connected vertices from e (expected 3): %d\n", fromE); + graph->addArc('a','b',9); + int fromE2 = graph->connectedVerticesCount('e'); + printf("Added ard from a to b, connected vertices from e (expected 4): %d\n", fromE2); - // Instantiating a DirectedGraph struct. -/* DirectedGraph* digraph = initialize_digraph(sizeof(char),"char"); + graph->addArc('d','a',83); + int fromE3 = graph->connectedVerticesCount('e'); + printf("Added arc from d to a, connected vertices from e (expected 4): %d\n", fromE3); + graph->enumerateVertices(); - // Creating elements to add to graph. - char* a = "a"; - char* b = "b"; - char* c = "c"; - char* d = "d"; - char* e = "e"; - char* f = "f"; + graph->addArc('d','e',0); + printf("Added arc from d to e"); + graph->enumerateVertices(); - // Adding elements to graph. - add_vertex(digraph, a); - add_vertex(digraph, b); - add_vertex(digraph, c); - add_vertex(digraph, d); - add_vertex(digraph, e); - add_vertex(digraph, f); + Vertex* source = graph->sourceVertex(); + std::cout << "Computing source vertex (expected a): " << source->getData() << "\n"; - // Creating edges between vertices and assigning weights. +/* // Creating edges between vertices and assigning weights. add_arc(digraph, a, b, 4); add_arc(digraph, a, e, 2); add_arc(digraph, a, f, 1); diff --git a/priority_queue.h b/priority_queue.h index d3b0ac0..d618a94 100644 --- a/priority_queue.h +++ b/priority_queue.h @@ -36,16 +36,16 @@ template PriorityQueue::PriorityQueue() { } template bool PriorityQueue::enqueue(T element, K priority) { - bool result = this->list->add_last(element); - this->priorities->add_last(priority); + bool result = this->list->addLast(element); + this->priorities->addLast(priority); sort(); return result; } template T PriorityQueue::dequeue() { - this->priorities->remove_first(); - return this->list->remove_first(); + this->priorities->removeFirst(); + return this->list->removeFirst(); } template T PriorityQueue::peek() { @@ -57,7 +57,7 @@ template int PriorityQueue::getSize() { } template bool PriorityQueue::contains(T element) { - return -1 != this->list->index_of(element); + return -1 != this->list->indexOf(element); } template void PriorityQueue::sort() { diff --git a/vertex.cpp b/vertex.cpp index 5c438dc..34437a8 100644 --- a/vertex.cpp +++ b/vertex.cpp @@ -29,8 +29,8 @@ Vertex* create_vertex(void* element) } /* - * This function adds an arc struct going from the Vertex* origin parameter to the Vertex* dest parameter - * with a weight of the float price parameter. + * This function adds an arc struct going from the Vertex* origin parameter + to the Vertex* dest parameter with a weight of the float price parameter. */ /* bool add_vertex_arc(Vertex* origin, Vertex* dest, float price) diff --git a/vertex.h b/vertex.h index aa800ac..1a0252d 100644 --- a/vertex.h +++ b/vertex.h @@ -2,7 +2,7 @@ * This vertex.h file provides the forward declarations for the vertex.c file. */ - +/* #ifndef __VERTEX_HEADER #define __VERTEX_HEADER @@ -50,3 +50,4 @@ float _get_arc_weight(Arc*); Vertex* get_arc_vertex(Arc*); #endif +*/ diff --git a/vertex0.h b/vertex0.h new file mode 100644 index 0000000..96ec17d --- /dev/null +++ b/vertex0.h @@ -0,0 +1,170 @@ +#pragma once +#include + +template class Vertex { + public: + Vertex(T data){ + this->data = data; + this->visited = false; + this->distance = 0; + this->parent = nullptr; + } + + // Sets the data class field of this Vertex. + void setData(T data){ this->data = data; } + + /* + * This function adds an arc struct going from the Vertex* origin parameter + * to the Vertex* dest parameter with a weight of the float price parameter. + */ + bool addArc(Vertex* destination, K weight) { + if(destination == nullptr) + return false; + + int prevSize = arcMap.size(); + + arcMap.insert({destination, weight}); + + if(this->arcMap.size() - prevSize == 1) { + return true; + } else { + return false; + } + } + + bool removeArc(Vertex* destination){ + if(destination == nullptr) + return false; + + /* for(int i = 0; i < prevSize; i++) { + Arc* arc = this->arcList->get(i); + + if(arc->vertex->data == destination->data) { + this->arcList->remove(i); + break; + } + } + */ + // this->arcMap::iterator it = this->arcMap->find(destination); + auto it = this->arcMap.find(destination); + if(it != this->arcMap.end()){ + this->arcMap.erase(destination); + return true; + } else { + return false; + } + } + + T getData() { + return this->data; + } + + std::map get_arc_list() { + return this->arcMap; + } + + bool getVisited() { + return this->visited; + } + + void setVisited(bool visit) { + this->visited = visit; + } + + /* + * The get_weight function takes a Vertex* origin vertex that goes to the + * Vertex* destination vertex parameter and returns the weight of that arc + * if such an arc exists. -1 is returned if no such arc exists. + */ + K getWeight(Vertex* destination) { + if(destination == nullptr) + return -1; + + /* + for(int i = 0; i < this->arcList->getSize(); i++) { + Arc* arc = this->arcList->get(i); + + // If the destination vertex is found in this vertex's arc list. + // MAY NEED TO COMPARE THE VERTICES AND NOT THE DATA BASED ON MEMORY ADDRESS + if(this->data == destination->data) { + return arc->weight; + } + } + */ + + auto it = this->arcMap.find(destination); + if(it != this->arcMap.end()){ + return this->arcMap[destination]; + } else { + return -1; + } + } + + /* + * The change_vertex_weight function takes an origin and destination Vertex* and changes the + * weight associated with the arc from the origin vertex to the destination vertex to the + * value of the cost parameter. + */ + bool changeWeight(Vertex* destination, K weight) { + if(destination == nullptr) + return false; + /* + for(int i = 0; i < this->arcList->getSize(); i++) { + Arc* arc = this->arcList->get(i); + + // If this arc's vertex is equal to the destination vertex + // MAY NEED TO COMPARE THE VERTICES AND NOT THE DATA BASED ON MEMORY ADDRESS + if(destination->data == arc->vertex->data) { + arc->setWeight(weight); + return true; + } + } + */ + + auto it = this->arcMap.find(destination); + if(it != this->arcMap.end()){ + this->arcMap[destination] = weight; + return true; + } else { + return false; + } + + // return false; + } + + void setDistance(K distance) { + this->distance = distance; + } + + K getDistance() { + return this->distance; + } + + bool hasArc(Vertex* v) { + auto it = this->arcMap.find(v); + if(it != this->arcMap.end()){ + return true; + } else { + return false; + } + } + + std::map getArcMap(){ + return this->arcMap; + } + + private: + T data; + bool visited; + K distance; + Vertex* parent; + std::map arcMap; + + void setParent(Vertex* v) { + this->parent = v; + } + + Vertex* getParent() { + return this->parent; + } +};