From 0a828878a86604d01c4249a1f08a0e7028d0eeb8 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 27 Aug 2023 08:38:08 -0400 Subject: [PATCH 01/32] Changed Digraph to class with nested Vertex and Arc classes --- directed_graph.h | 131 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 91 insertions(+), 40 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 632020c..3f25f1b 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -3,8 +3,7 @@ * for the directed_graph.c file. */ -#ifndef __DIRECTED_GRAPH_HEADER -#define __DIRECTED_GRAPH_HEADER +#pragma once #include #include @@ -17,41 +16,93 @@ #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 { + + class Vertex { + + public: + Vertex* create_vertex(void*); + bool add_vertex_arc(Vertex*, Vertex*, float); + bool remove_vertex_arc(Vertex*, Vertex*); + void* get_data(Vertex*); + LinkedList* get_arc_list(Vertex*); + bool been_visited(Vertex*); + void set_visited(Vertex*, bool); + float get_weight(Vertex*, Vertex*); + bool change_vertex_weight(Vertex*, Vertex*, float); + void set_vertex_distance(Vertex*, float); + float get_vertex_distance(Vertex*); + void set_vertex_parent(Vertex*, Vertex*); + Vertex* get_vertex_parent(Vertex*); + bool has_arc_to_vertex(Vertex*, Vertex*); + + LinkedList* arcList; + T data; + bool visited; + float distance; + Vertex* parent; + + private: + + }; + + class Arc { + public: + Arc* create_arc(Vertex*, float); + void set_arc_weight(Arc*, float); + void set_vertex(Arc*, Vertex*); + float _get_arc_weight(Arc*); + Vertex* get_arc_vertex(Arc*); + + Vertex* vertex; + float weight; + + private: + + }; + + public: + DirectedGraph(); + int getSize(); + 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); + + private: + LinkedList* vertexList; + int valueSize; + float** adjacencyMatrix; + +}; + +template DirectedGraph::DirectedGraph(){ + this->vertexList = new LinkedList(); +} +template int DirectedGraph::getSize(){ + +} + + From 701cf2ec944e228e8d39ac9c39362356278465fa Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 27 Aug 2023 08:51:07 -0400 Subject: [PATCH 02/32] updated getVertex, constructor, getSize --- directed_graph.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/directed_graph.h b/directed_graph.h index 3f25f1b..8b9d675 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -64,7 +64,7 @@ template class DirectedGraph { public: DirectedGraph(); int getSize(); - Vertex* get_vertex(DirectedGraph*, void*); + Vertex* getVertex(T); bool add_vertex(DirectedGraph*, void*); bool remove_vertex(DirectedGraph*, void*); LinkedList* get_vertices(DirectedGraph*); @@ -100,8 +100,32 @@ template class DirectedGraph { template DirectedGraph::DirectedGraph(){ this->vertexList = new LinkedList(); + this->adjacencyMatrix = nullptr; } template int DirectedGraph::getSize(){ + return this->vertexList->getSize(); +} + +template Vertex* getVertex(T value) { + if(value == nullptr) + return nullptr; + + 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->data == value) { + // Return this vertex pointer. + return v; + } + } + + return NULL; } From f8e39c9d2f61e2177155bbd5c4cd9c7d5653feb7 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 27 Aug 2023 09:03:29 -0400 Subject: [PATCH 03/32] Updated documentation and addVertex function --- directed_graph.h | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 8b9d675..2543fc3 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -64,8 +64,7 @@ template class DirectedGraph { public: DirectedGraph(); int getSize(); - Vertex* getVertex(T); - bool add_vertex(DirectedGraph*, void*); + bool addVertex(T); bool remove_vertex(DirectedGraph*, void*); LinkedList* get_vertices(DirectedGraph*); LinkedList* get_arcs(DirectedGraph*, void*); @@ -96,16 +95,29 @@ template class DirectedGraph { int valueSize; float** adjacencyMatrix; + Vertex* getVertex(T); + }; +/* + * 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, otherwise nullptr is returned. + */ template Vertex* getVertex(T value) { if(value == nullptr) return nullptr; @@ -119,14 +131,30 @@ template Vertex* getVertex(T value) { Vertex* v = this->vertexList->get(i); // If the data fieled variable in the vertex is equal to the element parameter. - if(v->data == value) { + if(v->data == value) { // Return this vertex pointer. return v; } } - return NULL; + 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){ + // If either parameter is NULL, return false. + if(element == nullptr) + return false; + + // Instantiate a Vertex struct while passing in the element parameter. + Vertex* v = create_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); } From c6ef0cef4d8ea53ae700f15b13c6d3b8e55e2298 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 27 Aug 2023 09:10:44 -0400 Subject: [PATCH 04/32] Updated removeVertex --- directed_graph.h | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/directed_graph.h b/directed_graph.h index 2543fc3..8970264 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -65,7 +65,7 @@ template class DirectedGraph { DirectedGraph(); int getSize(); bool addVertex(T); - bool remove_vertex(DirectedGraph*, void*); + bool removeVertex(T); LinkedList* get_vertices(DirectedGraph*); LinkedList* get_arcs(DirectedGraph*, void*); bool add_arc(DirectedGraph*, void*, void*, float); @@ -157,4 +157,31 @@ template bool DirectedGraph::addVertex(T element){ 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){ + // If element is a nullptr return false. + if(element == nullptr) + return false; + + // Store vertex list's current size in a local variable. + int prevSize = this->vertexList->getSize(); + + // 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->data == element) { + // Remove this vertex from the vertex list and break from loop. + this->vertexList->remove(i); + return true; + } + } + + return false; +} From 5f3db82561580ab36280ef9c2ec5be8dea841c0f Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 27 Aug 2023 09:34:18 -0400 Subject: [PATCH 05/32] Updated digraph to include types for storage T and weights K --- directed_graph.h | 30 ++++++++++++++++++------------ vertex.cpp | 4 ++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 8970264..68dfe58 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -16,7 +16,7 @@ #define MAX FLT_MAX -template class DirectedGraph { +template class DirectedGraph { class Vertex { @@ -66,8 +66,8 @@ template class DirectedGraph { int getSize(); bool addVertex(T); bool removeVertex(T); - LinkedList* get_vertices(DirectedGraph*); - LinkedList* get_arcs(DirectedGraph*, void*); + LinkedList* getVertices(); + 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); @@ -79,22 +79,21 @@ template class DirectedGraph { float get_arc_weight(DirectedGraph*, void*, void*); void reset_parent_links(DirectedGraph*); int compareVertex(void*, void*); - LinkedList* dijkstra(DirectedGraph*, 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*); + 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); - + private: LinkedList* vertexList; int valueSize; float** adjacencyMatrix; - Vertex* getVertex(T); }; @@ -102,7 +101,7 @@ template class DirectedGraph { /* * Constructs a Directed Graph object. */ -template DirectedGraph::DirectedGraph(){ +template DirectedGraph::DirectedGraph(){ this->vertexList = new LinkedList(); this->adjacencyMatrix = nullptr; } @@ -110,7 +109,7 @@ template DirectedGraph::DirectedGraph(){ /* * Returns the number of vertices in the Directed Graph. */ -template int DirectedGraph::getSize(){ +template int DirectedGraph::getSize(){ return this->vertexList->getSize(); } @@ -118,7 +117,7 @@ template int DirectedGraph::getSize(){ * Searches the Directed Graph for a vertex that contains the value parameter. * A pointer to that vertex is returned if found, otherwise nullptr is returned. */ -template Vertex* getVertex(T value) { +template Vertex* DirectedGraph::getVertex(T value){ if(value == nullptr) return nullptr; @@ -145,7 +144,7 @@ template Vertex* getVertex(T value) { * True is returned if the vertex was successfully added to the Directed Graph. Otherwie false * is returned. */ -template bool DirectedGraph::addVertex(T element){ +template bool DirectedGraph::addVertex(T element){ // If either parameter is NULL, return false. if(element == nullptr) return false; @@ -161,7 +160,7 @@ template bool DirectedGraph::addVertex(T element){ * The remove function removes a specific Vertex containing the element parameter from the Directed Graph, if found. */ -template bool DirectedGraph::removeVertex(T element){ +template bool DirectedGraph::removeVertex(T element){ // If element is a nullptr return false. if(element == nullptr) return false; @@ -185,3 +184,10 @@ template bool DirectedGraph::removeVertex(T element){ return false; } + +/* + * Returns the list of vertices in the Directed Graph. + */ +template LinkedList* DirectedGraph::getVertices(){ + return this->vertexList; +} 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) From c9400bc61093ebe9229c4e0666bc06822d0f9aab Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Mon, 28 Aug 2023 17:02:00 -0400 Subject: [PATCH 06/32] Updated the addArc method --- directed_graph.h | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 68dfe58..76d3bc5 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -66,9 +66,8 @@ template class DirectedGraph { int getSize(); bool addVertex(T); bool removeVertex(T); - LinkedList* getVertices(); LinkedList* get_arcs(DirectedGraph*, void*); - bool add_arc(DirectedGraph*, void*, void*, float); + bool addArc(T, T, K); bool remove_arc(DirectedGraph*, void*, void*); bool change_arc_weight(DirectedGraph*, void*, void*, float); int connected_vertices_count(DirectedGraph*, void*); @@ -95,6 +94,7 @@ template class DirectedGraph { int valueSize; float** adjacencyMatrix; Vertex* getVertex(T); + LinkedList* getVertices(); }; @@ -117,7 +117,7 @@ template int DirectedGraph::getSize(){ * Searches the Directed Graph for a vertex that contains the value parameter. * A pointer to that vertex is returned if found, otherwise nullptr is returned. */ -template Vertex* DirectedGraph::getVertex(T value){ +template Vertex* DirectedGraph::getVertex(T value){ if(value == nullptr) return nullptr; @@ -191,3 +191,25 @@ template bool DirectedGraph::removeVertex(T elemen 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) { + + // If either origin or destination parameter is nullptr, return false. + if(origin == nullptr || destination == nullptr) + return false; + + // Otherwise retrieve the Vertices associated with the origin and destination parameters. + Vertex* start = get_vertex(graph, origin); + Vertex* end = get_vertex(graph, destination); + + // Return the call to add vertex arc function with the two vertices and the arc weight. + return add_vertex_arc(start, end, cost); +} From 2b55480c0489d10f9932064e0c1aefb5fe674386 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Mon, 28 Aug 2023 17:07:26 -0400 Subject: [PATCH 07/32] Implemented Vertex and Arc constructors --- directed_graph.cpp | 3 ++- directed_graph.h | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/directed_graph.cpp b/directed_graph.cpp index c4aa70b..62f723e 100644 --- a/directed_graph.cpp +++ b/directed_graph.cpp @@ -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 76d3bc5..81f47d6 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -11,7 +11,7 @@ #include #include #include "linked_list.h" -#include "vertex.h" +//#include "vertex.h" #include "priority_queue.h" #define MAX FLT_MAX @@ -21,7 +21,11 @@ template class DirectedGraph { class Vertex { public: - Vertex* create_vertex(void*); + Vertex Vertex(T data){ + this->data = data; + this->arcList = new LinkedList(); + } + bool add_vertex_arc(Vertex*, Vertex*, float); bool remove_vertex_arc(Vertex*, Vertex*); void* get_data(Vertex*); @@ -36,7 +40,7 @@ template class DirectedGraph { Vertex* get_vertex_parent(Vertex*); bool has_arc_to_vertex(Vertex*, Vertex*); - LinkedList* arcList; + LinkedList* arcList; T data; bool visited; float distance; @@ -48,14 +52,18 @@ template class DirectedGraph { class Arc { public: - Arc* create_arc(Vertex*, float); + Arc Arc(Vertex* vertex, K weight) { + this->vertex = vertex; + this->weight = weight; + } + void set_arc_weight(Arc*, float); void set_vertex(Arc*, Vertex*); float _get_arc_weight(Arc*); Vertex* get_arc_vertex(Arc*); Vertex* vertex; - float weight; + K weight; private: From 1b79be12f44fafec23720adc5e0704e197778a85 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Mon, 28 Aug 2023 17:37:02 -0400 Subject: [PATCH 08/32] Implemented Vertex and Arc getters and setters --- directed_graph.h | 97 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 28 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 81f47d6..d73f61b 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -19,53 +19,94 @@ template class DirectedGraph { class Vertex { - public: - Vertex Vertex(T data){ + Vertex(T data){ this->data = data; this->arcList = new LinkedList(); + this->visited = false; + this->distance = 0; + this->parent = nullptr; + } + + bool addArc(Vertex* v, K weight) { + + } + + bool removeArc(Vertex* v){ + + } + + T getData() { + return this->data; } - bool add_vertex_arc(Vertex*, Vertex*, float); - bool remove_vertex_arc(Vertex*, Vertex*); - void* get_data(Vertex*); LinkedList* get_arc_list(Vertex*); - bool been_visited(Vertex*); - void set_visited(Vertex*, bool); - float get_weight(Vertex*, Vertex*); + + bool getVisited() { + return this->visited; + } + + void setVisited(bool visit) { + this->visited = visit; + } + + float getWeight(Vertex*, Vertex*); bool change_vertex_weight(Vertex*, Vertex*, float); - void set_vertex_distance(Vertex*, float); - float get_vertex_distance(Vertex*); - void set_vertex_parent(Vertex*, Vertex*); - Vertex* get_vertex_parent(Vertex*); - bool has_arc_to_vertex(Vertex*, Vertex*); - - LinkedList* arcList; - T data; - bool visited; - float distance; - Vertex* parent; - private: + void setDistance(K distance) { + this->distance = distance; + } + + K getDistance() { + return this->distance; + } + void setParent(Vertex* v) { + this->parent = v; + } + + Vertex* getParent() { + return this->parent; + } + + bool hasArc(Vertex* v) { + + } + + private: + LinkedList* arcList; + T data; + bool visited; + K distance; + Vertex* parent; }; class Arc { public: - Arc Arc(Vertex* vertex, K weight) { + Arc(Vertex* vertex, K weight) { this->vertex = vertex; this->weight = weight; } - void set_arc_weight(Arc*, float); - void set_vertex(Arc*, Vertex*); - float _get_arc_weight(Arc*); - Vertex* get_arc_vertex(Arc*); - - Vertex* vertex; - K weight; + void setWeight(K weight) { + this->weight = weight; + } + + void setVertex(Vertex* vertex){ + this->vertex = vertex; + } + K getWeight(){ + return this->weight; + } + + Vertex* getVertex() { + return this->vertex; + } + private: + Vertex* vertex; + K weight; }; From 4a429572f8b6d335d53cdb0e771a5b1533c6497c Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Mon, 28 Aug 2023 18:26:33 -0400 Subject: [PATCH 09/32] Updated Vertex removeArc method --- directed_graph.h | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index d73f61b..19d4fe7 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -28,12 +28,43 @@ template class DirectedGraph { this->parent = nullptr; } - bool addArc(Vertex* v, K weight) { + bool addArc(Vertex* destination, K weight) { + if(destination == nullptr) + return false; - } + Arc* arc = new Arc(destination, price); + + int prevSize = this->arcList->getSize(); + + this->arcList->add_last(arc); - bool removeArc(Vertex* v){ + if(this->arcList->getSize() - prevSize == 1) { + return true; + } else { + return false; + } + } + bool removeArc(Vertex* destination){ + if(destination == nullptr) + return false; + + int prevSize = this->arcList->getSize(); + + for(int i = 0; i < prevSize; i++) { + Arc* arc = this->arcList->get(i); + + if(arc->vertex->data == destination->data) { + this->arcList->remove(i); + break; + } + } + + if(prevSize - this->arcList->getSize() == 1) { + return true; + } else { + return false; + } } T getData() { From 3b69934e52ad7160997366cb4b3df5c298d26067 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Mon, 28 Aug 2023 18:47:10 -0400 Subject: [PATCH 10/32] Updated Vertex getWeight and changeWeight functions --- directed_graph.h | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 19d4fe7..6a1eef7 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -71,7 +71,9 @@ template class DirectedGraph { return this->data; } - LinkedList* get_arc_list(Vertex*); + LinkedList* get_arc_list() { + return this->arcList; + } bool getVisited() { return this->visited; @@ -81,8 +83,40 @@ template class DirectedGraph { this->visited = visit; } - float getWeight(Vertex*, Vertex*); - bool change_vertex_weight(Vertex*, Vertex*, float); + 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; + } + } + + return -1; + } + + 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; + } + } + + return false; + } void setDistance(K distance) { this->distance = distance; From f27d01d32c993a0711331941a32eadf707dd6959 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Mon, 28 Aug 2023 18:51:18 -0400 Subject: [PATCH 11/32] Copied over documentation for Vertex functions --- directed_graph.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/directed_graph.h b/directed_graph.h index 6a1eef7..126bd8d 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -28,6 +28,10 @@ template class DirectedGraph { this->parent = nullptr; } + /* + * 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; @@ -83,6 +87,11 @@ template class DirectedGraph { 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; @@ -100,6 +109,11 @@ template class DirectedGraph { 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; @@ -172,7 +186,6 @@ template class DirectedGraph { private: Vertex* vertex; K weight; - }; public: From 8cbc0c61fb2fd4bdbbbac921ff1caa77763044ab Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Tue, 29 Aug 2023 18:41:10 -0400 Subject: [PATCH 12/32] Updated removeArc and commnted out Vertex.h --- directed_graph.h | 19 ++++++++++++++++--- vertex.h | 3 ++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 126bd8d..e075e8f 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -1,6 +1,6 @@ /* - * 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. */ #pragma once @@ -195,7 +195,7 @@ template class DirectedGraph { bool removeVertex(T); LinkedList* get_arcs(DirectedGraph*, void*); bool addArc(T, T, K); - bool remove_arc(DirectedGraph*, void*, void*); + bool removeArc(T, T); bool change_arc_weight(DirectedGraph*, void*, void*, float); int connected_vertices_count(DirectedGraph*, void*); int _connected_vertices_count_recursive(DirectedGraph*, void*); @@ -340,3 +340,16 @@ template bool DirectedGraph::addArc(T origin, T des // Return the call to add vertex arc function with the two vertices and the arc weight. return add_vertex_arc(start, 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) { + if(origin == nullptr || destination == nullptr) + return false; + + Vertex* start = getVertex(origin); + return start->removeArc(destination); +} \ No newline at end of file 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 +*/ From 15055130a16ff2c465c073197267bd2815894681 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Tue, 29 Aug 2023 18:46:16 -0400 Subject: [PATCH 13/32] Updated setVisitedField for the graph object --- directed_graph.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/directed_graph.h b/directed_graph.h index e075e8f..2dafc83 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -200,7 +200,7 @@ template class DirectedGraph { int connected_vertices_count(DirectedGraph*, void*); int _connected_vertices_count_recursive(DirectedGraph*, void*); void* source_vertex(DirectedGraph*); - void set_visited_field(DirectedGraph*, bool); + void setVisitedField(bool); void buildTree(DirectedGraph*, void*); float get_arc_weight(DirectedGraph*, void*, void*); void reset_parent_links(DirectedGraph*); @@ -352,4 +352,18 @@ template bool DirectedGraph::removeArc(T origin, T Vertex* start = getVertex(origin); return start->removeArc(destination); +} + +/* + * 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; i++) { + // Retrieve the vertex struct at index i. + Vertex* v = this->vertexList->get(i); + v->setVisited(value); + } } \ No newline at end of file From 18f8be91204559ab3c8b26baf76ccbd44578f203 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Tue, 29 Aug 2023 19:01:58 -0400 Subject: [PATCH 14/32] Resolving compilation errors --- directed_graph.h | 10 +++++----- main.cpp | 7 ++++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 2dafc83..de09e73 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -36,7 +36,7 @@ template class DirectedGraph { if(destination == nullptr) return false; - Arc* arc = new Arc(destination, price); + Arc* arc = new Arc(destination, weight); int prevSize = this->arcList->getSize(); @@ -214,7 +214,7 @@ template class DirectedGraph { float extract_value(int, int, char*); float* float_arr_from_str(char*); int value_count(char*); - std::ostream& operator<<(std::ostream& o, DirectedGraph& net); + //std::ostream& operator<<(std::ostream& o, DirectedGraph& net); private: LinkedList* vertexList; @@ -334,11 +334,11 @@ template bool DirectedGraph::addArc(T origin, T des return false; // Otherwise retrieve the Vertices associated with the origin and destination parameters. - Vertex* start = get_vertex(graph, origin); - Vertex* end = get_vertex(graph, destination); + 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 add_vertex_arc(start, end, cost); + return start->addArc(end, cost); } diff --git a/main.cpp b/main.cpp index 9d0dcd9..2978910 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 @@ -129,6 +129,11 @@ int main(int argc, char** argv) std::cout << "Dequeue (expected Fluffy) " << pq->dequeue()->getName()<< "\n"; std::cout << "list size (expected 0): " << pq->getSize() << "\n"; + printf("\t.....DiGraph Testing.....\n"); + + DirectedGraph* graph = new DirectedGraph(); + + /* LinkedList* list = new LinkedList(); From 8db004ca14ba35e22a44d0388083bc081856082b Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sat, 2 Sep 2023 13:36:22 -0400 Subject: [PATCH 15/32] Created new file for new vertex implementation --- vertex0.h | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 vertex0.h diff --git a/vertex0.h b/vertex0.h new file mode 100644 index 0000000..e69de29 From 28ed610f5903152c9da133116ba295ba770c13fc Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sat, 2 Sep 2023 14:08:57 -0400 Subject: [PATCH 16/32] Updating implementations to reflect map and not LinkedList --- vertex0.h | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/vertex0.h b/vertex0.h index e69de29..038e240 100644 --- a/vertex0.h +++ b/vertex0.h @@ -0,0 +1,153 @@ +#pragma once +#include + +template class Vertex { + public: + Vertex(T data){ + this->data = data; + this->visited = false; + this->distance = 0; + this->parent = nullptr + this->arcMap = new std::map(); + ; + } + + /* + * 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; + + Vertex* arc = new Vertex(weight); + int prevSize = this->arcMap->size(); + + this->arcMap->pair_insert(arc, 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; + 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; + } + } + */ + + this->arcMap::iterator it; + 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; + } + } + + return false; + } + + void setDistance(K distance) { + this->distance = distance; + } + + K getDistance() { + return this->distance; + } + + bool hasArc(Vertex* v) { + + } + + private: + std::map arcMap; + T data; + bool visited; + K distance; + Vertex* parent; + + void setParent(Vertex* v) { + this->parent = v; + } + + Vertex* getParent() { + return this->parent; + } +}; From 745144f861eda139192c045abd8bc2cd85a83972 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sat, 2 Sep 2023 14:11:41 -0400 Subject: [PATCH 17/32] Updating more implementations to reflect map and not LinkedList --- vertex0.h | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/vertex0.h b/vertex0.h index 038e240..28a2549 100644 --- a/vertex0.h +++ b/vertex0.h @@ -109,7 +109,7 @@ template class Vertex { 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); @@ -120,8 +120,18 @@ template class Vertex { return true; } } + */ + + this->arcMap::iterator it; + it = this->arcMap->find(destination); + if(it != this->arcMap->end()){ + this->arcMap[destination] = weight; + return true; + } else { + return false; + } - return false; + // return false; } void setDistance(K distance) { @@ -133,15 +143,21 @@ template class Vertex { } bool hasArc(Vertex* v) { - - } + this->arcMap::iterator it; + it = this->arcMap->find(v); + if(it != this->arcMap->end()){ + return true; + } else { + return false; + } + } private: - std::map arcMap; T data; bool visited; K distance; Vertex* parent; + std::map arcMap; void setParent(Vertex* v) { this->parent = v; From a8d1c706581d6298309d23da8a48e0d1a1d1f68a Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sat, 2 Sep 2023 14:14:55 -0400 Subject: [PATCH 18/32] removed Vertex and Arc class dec and imp from digraph header --- directed_graph.h | 171 ----------------------------------------------- 1 file changed, 171 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index de09e73..dc43255 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -17,177 +17,6 @@ #define MAX FLT_MAX template class DirectedGraph { - - class Vertex { - public: - Vertex(T data){ - this->data = data; - this->arcList = new LinkedList(); - this->visited = false; - this->distance = 0; - this->parent = nullptr; - } - - /* - * 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; - - Arc* arc = new Arc(destination, weight); - - int prevSize = this->arcList->getSize(); - - this->arcList->add_last(arc); - - if(this->arcList->getSize() - prevSize == 1) { - return true; - } else { - return false; - } - } - - bool removeArc(Vertex* destination){ - if(destination == nullptr) - return false; - - int prevSize = this->arcList->getSize(); - - for(int i = 0; i < prevSize; i++) { - Arc* arc = this->arcList->get(i); - - if(arc->vertex->data == destination->data) { - this->arcList->remove(i); - break; - } - } - - if(prevSize - this->arcList->getSize() == 1) { - return true; - } else { - return false; - } - } - - T getData() { - return this->data; - } - - LinkedList* get_arc_list() { - return this->arcList; - } - - 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; - } - } - - 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; - } - } - - return false; - } - - void setDistance(K distance) { - this->distance = distance; - } - - K getDistance() { - return this->distance; - } - - void setParent(Vertex* v) { - this->parent = v; - } - - Vertex* getParent() { - return this->parent; - } - - bool hasArc(Vertex* v) { - - } - - private: - LinkedList* arcList; - T data; - bool visited; - K distance; - Vertex* parent; - }; - - class Arc { - public: - Arc(Vertex* vertex, K weight) { - this->vertex = vertex; - this->weight = weight; - } - - void setWeight(K weight) { - this->weight = weight; - } - - void setVertex(Vertex* vertex){ - this->vertex = vertex; - } - - K getWeight(){ - return this->weight; - } - - Vertex* getVertex() { - return this->vertex; - } - - private: - Vertex* vertex; - K weight; - }; - public: DirectedGraph(); int getSize(); From 079adad682b291370854c0c6fc61978a6b3a8743 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sat, 2 Sep 2023 14:31:31 -0400 Subject: [PATCH 19/32] Resolving compilation errors --- directed_graph.h | 19 ++++++++++--------- vertex0.h | 15 ++++++--------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index dc43255..246ad07 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -12,6 +12,7 @@ #include #include "linked_list.h" //#include "vertex.h" +#include "vertex0.h" #include "priority_queue.h" #define MAX FLT_MAX @@ -49,7 +50,7 @@ template class DirectedGraph { LinkedList* vertexList; int valueSize; float** adjacencyMatrix; - Vertex* getVertex(T); + Vertex* getVertex(T); LinkedList* getVertices(); }; @@ -73,7 +74,7 @@ template int DirectedGraph::getSize(){ * Searches the Directed Graph for a vertex that contains the value parameter. * A pointer to that vertex is returned if found, otherwise nullptr is returned. */ -template Vertex* DirectedGraph::getVertex(T value){ +template Vertex* DirectedGraph::getVertex(T value){ if(value == nullptr) return nullptr; @@ -83,7 +84,7 @@ template Vertex* DirectedGraph::getVertex(T value for(int i = 0; i < size; i++) { // Retrieve the vertex at index i in the vertex list. - Vertex* v = this->vertexList->get(i); + Vertex* v = this->vertexList->get(i); // If the data fieled variable in the vertex is equal to the element parameter. if(v->data == value) { @@ -106,7 +107,7 @@ template bool DirectedGraph::addVertex(T element){ return false; // Instantiate a Vertex struct while passing in the element parameter. - Vertex* v = create_vertex(element); + Vertex* v = create_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); @@ -128,7 +129,7 @@ template bool DirectedGraph::removeVertex(T elemen for(int i = 0; i < prevSize; i++) { // Retrieve the vertex in the vertex list at index i. - Vertex* v = this->vertexList->get(i); + Vertex* v = this->vertexList->get(i); // If this vertex is equivalent to the element parameter if(v->data == element) { @@ -163,8 +164,8 @@ template bool DirectedGraph::addArc(T origin, T des return false; // Otherwise retrieve the Vertices associated with the origin and destination parameters. - Vertex* start = getVertex(origin); - Vertex* end = getVertex(destination); + 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); @@ -179,7 +180,7 @@ template bool DirectedGraph::removeArc(T origin, T if(origin == nullptr || destination == nullptr) return false; - Vertex* start = getVertex(origin); + Vertex* start = getVertex(origin); return start->removeArc(destination); } @@ -192,7 +193,7 @@ template void DirectedGraph::setVisitedField(bool // For each vertex in the Directed Graph's vertex list. for(int i = 0; i < this->vertexList; i++) { // Retrieve the vertex struct at index i. - Vertex* v = this->vertexList->get(i); + Vertex* v = this->vertexList->get(i); v->setVisited(value); } } \ No newline at end of file diff --git a/vertex0.h b/vertex0.h index 28a2549..020b035 100644 --- a/vertex0.h +++ b/vertex0.h @@ -7,7 +7,7 @@ template class Vertex { this->data = data; this->visited = false; this->distance = 0; - this->parent = nullptr + this->parent = nullptr; this->arcMap = new std::map(); ; } @@ -45,8 +45,8 @@ template class Vertex { } } */ - this->arcMap::iterator it; - it = this->arcMap->find(destination); + // 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; @@ -92,8 +92,7 @@ template class Vertex { } */ - this->arcMap::iterator it; - it = this->arcMap->find(destination); + auto it = this->arcMap->find(destination); if(it != this->arcMap->end()){ return this->arcMap[destination]; } else { @@ -122,8 +121,7 @@ template class Vertex { } */ - this->arcMap::iterator it; - it = this->arcMap->find(destination); + auto it = this->arcMap->find(destination); if(it != this->arcMap->end()){ this->arcMap[destination] = weight; return true; @@ -143,8 +141,7 @@ template class Vertex { } bool hasArc(Vertex* v) { - this->arcMap::iterator it; - it = this->arcMap->find(v); + auto it = this->arcMap->find(v); if(it != this->arcMap->end()){ return true; } else { From 29e3a64ccabbc3c0e7df45ecbc24be2828f4cb07 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 3 Sep 2023 09:38:44 -0400 Subject: [PATCH 20/32] Updated linkedList interface and resolving errors --- directed_graph.h | 13 +++++-------- linked_list.h | 38 +++++++++++++++++++------------------- main.cpp | 38 ++++++++++++++++++++++---------------- priority_queue.h | 10 +++++----- 4 files changed, 51 insertions(+), 48 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 246ad07..d7d1bee 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -47,11 +47,11 @@ template class DirectedGraph { //std::ostream& operator<<(std::ostream& o, DirectedGraph& net); private: - LinkedList* vertexList; + LinkedList*>* vertexList; int valueSize; float** adjacencyMatrix; Vertex* getVertex(T); - LinkedList* getVertices(); + LinkedList*>* getVertices(); }; @@ -59,7 +59,7 @@ template class DirectedGraph { * Constructs a Directed Graph object. */ template DirectedGraph::DirectedGraph(){ - this->vertexList = new LinkedList(); + this->vertexList = new LinkedList*>(); this->adjacencyMatrix = nullptr; } @@ -102,12 +102,9 @@ template Vertex* DirectedGraph::getVertex(T * is returned. */ template bool DirectedGraph::addVertex(T element){ - // If either parameter is NULL, return false. - if(element == nullptr) - return false; // Instantiate a Vertex struct while passing in the element parameter. - Vertex* v = create_vertex(element); + 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); @@ -145,7 +142,7 @@ template bool DirectedGraph::removeVertex(T elemen /* * Returns the list of vertices in the Directed Graph. */ -template LinkedList* DirectedGraph::getVertices(){ +template LinkedList*>* DirectedGraph::getVertices(){ return this->vertexList; } 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 2978910..d877ffb 100644 --- a/main.cpp +++ b/main.cpp @@ -32,9 +32,9 @@ int main(int argc, char** argv) 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,7 +123,7 @@ 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"; @@ -133,6 +133,12 @@ int main(int argc, char** argv) DirectedGraph* graph = new DirectedGraph(); + graph->addVertex('a'); + graph->addVertex('b'); + graph->addVertex('c'); + graph->addVertex('d'); + graph->addVertex('e'); + graph->addVertex('f'); /* LinkedList* list = new LinkedList(); 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() { From ff17581d4a00a1eae60285606aef3741fe75d575 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 1 Oct 2023 11:08:27 -0400 Subject: [PATCH 21/32] Big fix for Vertex map initialization --- vertex0.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/vertex0.h b/vertex0.h index 020b035..922ea21 100644 --- a/vertex0.h +++ b/vertex0.h @@ -7,9 +7,7 @@ template class Vertex { this->data = data; this->visited = false; this->distance = 0; - this->parent = nullptr; - this->arcMap = new std::map(); - ; + this->parent = nullptr; } /* From daf3a506c49cd110f09f3e6c92492b8b65f4dd78 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 1 Oct 2023 13:28:54 -0400 Subject: [PATCH 22/32] Resolved vertex and digraph addArc function bugs --- directed_graph.h | 24 +++++++++++++++++----- main.cpp | 52 +++++------------------------------------------- vertex0.h | 12 +++++++---- 3 files changed, 32 insertions(+), 56 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index d7d1bee..cc6350b 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -45,6 +45,7 @@ template class DirectedGraph { float* float_arr_from_str(char*); int value_count(char*); //std::ostream& operator<<(std::ostream& o, DirectedGraph& net); + void enumerateVertices(); private: LinkedList*>* vertexList; @@ -72,11 +73,9 @@ template int DirectedGraph::getSize(){ /* * Searches the Directed Graph for a vertex that contains the value parameter. - * A pointer to that vertex is returned if found, otherwise nullptr is returned. + * A pointer to that vertex is returned if found. */ template Vertex* DirectedGraph::getVertex(T value){ - if(value == nullptr) - return nullptr; int size = this->vertexList->getSize(); @@ -87,7 +86,7 @@ template Vertex* DirectedGraph::getVertex(T Vertex* v = this->vertexList->get(i); // If the data fieled variable in the vertex is equal to the element parameter. - if(v->data == value) { + if(v->getData() == value) { // Return this vertex pointer. return v; } @@ -157,8 +156,9 @@ template LinkedList*>* DirectedGraph:: template bool DirectedGraph::addArc(T origin, T destination, K cost) { // If either origin or destination parameter is nullptr, return false. - if(origin == nullptr || destination == nullptr) +/* if(origin == NULL || destination == NULL) return false; + */ // Otherwise retrieve the Vertices associated with the origin and destination parameters. Vertex* start = getVertex(origin); @@ -193,4 +193,18 @@ template void DirectedGraph::setVisitedField(bool Vertex* v = this->vertexList->get(i); v->setVisited(value); } +} + +template void DirectedGraph::enumerateVertices() { + + // 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); + std::map*, K> vMap = v->getArcMap(); + std::cout << v->getData() << ":\n"; + int count = 0; + for(auto j = vMap.begin(); j != vMap.end(); j++){ + std::cout << " " << j->second << "\n"; + } + } } \ No newline at end of file diff --git a/main.cpp b/main.cpp index d877ffb..92d5903 100644 --- a/main.cpp +++ b/main.cpp @@ -140,55 +140,13 @@ int main(int argc, char** argv) graph->addVertex('e'); graph->addVertex('f'); -/* - LinkedList* list = new LinkedList(); + // Creating Edges between vertices and assigning weights + graph->addArc('a','b',34); - list->add_first(0); - for(int i = 1; i < 99; i++){ - list->add_at(i,i); - } - list->add_last(99); - - int size = list->getSize(); - - for (int i = 0; i < size; ++i) { - int item = list->get(i); - printf("%d\n", item); - } - - int first = list->remove_first(); - int last = list->remove_last(); - - printf("first %d\n", first); - printf("last %d\n", last); - - for (int i = list->getSize()-1; i >=0; i--){ - int element = list->remove(i); - printf("%d\n", element); - } -*/ - - - // Instantiating a DirectedGraph struct. -/* DirectedGraph* digraph = initialize_digraph(sizeof(char),"char"); - - // Creating elements to add to graph. - char* a = "a"; - char* b = "b"; - char* c = "c"; - char* d = "d"; - char* e = "e"; - char* f = "f"; - - // 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); + printf("\t.....Enumerating Connections in DiGraph.....\n"); + graph->enumerateVertices(); - // 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/vertex0.h b/vertex0.h index 922ea21..c83cdb2 100644 --- a/vertex0.h +++ b/vertex0.h @@ -18,12 +18,12 @@ template class Vertex { if(destination == nullptr) return false; - Vertex* arc = new Vertex(weight); - int prevSize = this->arcMap->size(); + Vertex* arc = new Vertex(weight); + int prevSize = arcMap.size(); - this->arcMap->pair_insert(arc, weight); + arcMap.insert({arc, weight}); - if(this->arcMap->size() - prevSize == 1) { + if(this->arcMap.size() - prevSize == 1) { return true; } else { return false; @@ -147,6 +147,10 @@ template class Vertex { } } + std::map getArcMap(){ + return this->arcMap; + } + private: T data; bool visited; From a3eca9e4e6810cbb9d97e79ec5d3593ce19395de Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 1 Oct 2023 15:13:32 -0400 Subject: [PATCH 23/32] Bug fix with addArc in vertex --- directed_graph.h | 7 ++++--- main.cpp | 8 +++++++- vertex0.h | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index cc6350b..48513c2 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -200,11 +200,12 @@ template void DirectedGraph::enumerateVertices() { // 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); - std::map*, K> vMap = v->getArcMap(); + std::map*, K> map = v->getArcMap(); std::cout << v->getData() << ":\n"; int count = 0; - for(auto j = vMap.begin(); j != vMap.end(); j++){ - std::cout << " " << j->second << "\n"; + for(auto j = map.begin(); j != map.end(); j++){ + Vertex* vx = j->first; + std::cout << "\t" << vx->getData() << ": " << j->second << "\n"; } } } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 92d5903..6853986 100644 --- a/main.cpp +++ b/main.cpp @@ -141,7 +141,13 @@ int main(int argc, char** argv) graph->addVertex('f'); // Creating Edges between vertices and assigning weights - graph->addArc('a','b',34); + 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('c','d',1); + graph->addArc('f','d',0); printf("\t.....Enumerating Connections in DiGraph.....\n"); graph->enumerateVertices(); diff --git a/vertex0.h b/vertex0.h index c83cdb2..f78b054 100644 --- a/vertex0.h +++ b/vertex0.h @@ -18,10 +18,10 @@ template class Vertex { if(destination == nullptr) return false; - Vertex* arc = new Vertex(weight); + //Vertex* arc = new Vertex(destination); int prevSize = arcMap.size(); - arcMap.insert({arc, weight}); + arcMap.insert({destination, weight}); if(this->arcMap.size() - prevSize == 1) { return true; From 60c5333fa40904eafbc3a9d929b5c0ea3181cbd2 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 1 Oct 2023 15:24:42 -0400 Subject: [PATCH 24/32] debugging digraph and vertex removeArc function --- directed_graph.h | 23 ++++++----------------- main.cpp | 8 ++++++++ vertex0.h | 19 +++++++++---------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 48513c2..52ca2ff 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -114,10 +114,7 @@ template bool DirectedGraph::addVertex(T element){ element parameter from the Directed Graph, if found. */ template bool DirectedGraph::removeVertex(T element){ - // If element is a nullptr return false. - if(element == nullptr) - return false; - + // Store vertex list's current size in a local variable. int prevSize = this->vertexList->getSize(); @@ -155,12 +152,7 @@ template LinkedList*>* DirectedGraph:: */ template bool DirectedGraph::addArc(T origin, T destination, K cost) { - // If either origin or destination parameter is nullptr, return false. -/* if(origin == NULL || destination == NULL) - return false; - */ - - // Otherwise retrieve the Vertices associated with the origin and destination parameters. + // Retrieve the Vertices associated with the origin and destination parameters. Vertex* start = getVertex(origin); Vertex* end = getVertex(destination); @@ -174,11 +166,10 @@ template bool DirectedGraph::addArc(T origin, T des * parameters. */ template bool DirectedGraph::removeArc(T origin, T destination) { - if(origin == nullptr || destination == nullptr) - return false; - + Vertex* start = getVertex(origin); - return start->removeArc(destination); + Vertex* end = getVertex(destination); + return start->removeArc(end); } /* @@ -202,10 +193,8 @@ template void DirectedGraph::enumerateVertices() { Vertex* v = this->vertexList->get(i); std::map*, K> map = v->getArcMap(); std::cout << v->getData() << ":\n"; - int count = 0; for(auto j = map.begin(); j != map.end(); j++){ - Vertex* vx = j->first; - std::cout << "\t" << vx->getData() << ": " << j->second << "\n"; + std::cout << "\t" << j->first->getData() << ": " << j->second << "\n"; } } } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 6853986..22af291 100644 --- a/main.cpp +++ b/main.cpp @@ -146,12 +146,20 @@ int main(int argc, char** argv) 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); printf("\t.....Enumerating Connections in DiGraph.....\n"); graph->enumerateVertices(); + + printf("\t.....Removing connection from a to b and from f to d.....\n"); + graph->removeArc('a','b'); + graph->removeArc('f','d'); + graph->enumerateVertices(); + /* // Creating edges between vertices and assigning weights. add_arc(digraph, a, b, 4); add_arc(digraph, a, e, 2); diff --git a/vertex0.h b/vertex0.h index f78b054..693b739 100644 --- a/vertex0.h +++ b/vertex0.h @@ -18,7 +18,6 @@ template class Vertex { if(destination == nullptr) return false; - //Vertex* arc = new Vertex(destination); int prevSize = arcMap.size(); arcMap.insert({destination, weight}); @@ -44,9 +43,9 @@ template class Vertex { } */ // this->arcMap::iterator it = this->arcMap->find(destination); - auto it = this->arcMap->find(destination); - if(it != this->arcMap->end()){ - this->arcMap->erase(destination); + auto it = this->arcMap.find(destination); + if(it != this->arcMap.end()){ + this->arcMap.erase(destination); return true; } else { return false; @@ -90,8 +89,8 @@ template class Vertex { } */ - auto it = this->arcMap->find(destination); - if(it != this->arcMap->end()){ + auto it = this->arcMap.find(destination); + if(it != this->arcMap.end()){ return this->arcMap[destination]; } else { return -1; @@ -119,8 +118,8 @@ template class Vertex { } */ - auto it = this->arcMap->find(destination); - if(it != this->arcMap->end()){ + auto it = this->arcMap.find(destination); + if(it != this->arcMap.end()){ this->arcMap[destination] = weight; return true; } else { @@ -139,8 +138,8 @@ template class Vertex { } bool hasArc(Vertex* v) { - auto it = this->arcMap->find(v); - if(it != this->arcMap->end()){ + auto it = this->arcMap.find(v); + if(it != this->arcMap.end()){ return true; } else { return false; From dfb29f0e08ad103ec6bdd154cb8de2f7e72d5947 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sat, 7 Oct 2023 10:09:31 -0400 Subject: [PATCH 25/32] Tested and fixed removeVertex --- directed_graph.h | 33 ++++++++++++++++++++++----------- main.cpp | 23 +++++++++++++++++++---- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 52ca2ff..3599b4b 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -19,13 +19,13 @@ template class DirectedGraph { public: - DirectedGraph(); + DirectedGraph(); // Tested int getSize(); - bool addVertex(T); - bool removeVertex(T); + bool addVertex(T); // Tested + bool removeVertex(T); // Tested LinkedList* get_arcs(DirectedGraph*, void*); - bool addArc(T, T, K); - bool removeArc(T, T); + bool addArc(T, T, K); // Tested + bool removeArc(T, T); // Tested bool change_arc_weight(DirectedGraph*, void*, void*, float); int connected_vertices_count(DirectedGraph*, void*); int _connected_vertices_count_recursive(DirectedGraph*, void*); @@ -117,22 +117,33 @@ template bool DirectedGraph::removeVertex(T elemen // 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->data == element) { - // Remove this vertex from the vertex list and break from loop. + // 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); - return true; + found = true; + break; } } - return false; + 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; + } } /* diff --git a/main.cpp b/main.cpp index 22af291..060e049 100644 --- a/main.cpp +++ b/main.cpp @@ -25,7 +25,7 @@ class Animal{ int main(int argc, char** argv) { - +/* LinkedList AList = LinkedList(); Animal* a = new Animal("Fluffy"); @@ -128,11 +128,13 @@ int main(int argc, char** argv) 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.....\n"); +*/ + printf("\t.....DiGraph Testing (constructor).....\n"); DirectedGraph* graph = new DirectedGraph(); + printf("....Adding Vertices (addVertex)....\n"); + graph->addVertex('a'); graph->addVertex('b'); graph->addVertex('c'); @@ -141,6 +143,8 @@ int main(int argc, char** argv) graph->addVertex('f'); // Creating Edges between vertices and assigning weights + + printf("....Adding Arcs (addArc)....\n"); graph->addArc('a','b',1); graph->addArc('a','c',2); graph->addArc('a','d',7); @@ -155,11 +159,22 @@ int main(int argc, char** argv) graph->enumerateVertices(); - printf("\t.....Removing connection from a to b and from f to d.....\n"); + 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); + /* // Creating edges between vertices and assigning weights. add_arc(digraph, a, b, 4); add_arc(digraph, a, e, 2); From e5cd2395f2df458a1474be50afedc350489c03e2 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sat, 7 Oct 2023 10:22:57 -0400 Subject: [PATCH 26/32] Fixed and tested changeArcWeight --- directed_graph.h | 10 ++++++++-- main.cpp | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 3599b4b..152c8e1 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -20,13 +20,13 @@ template class DirectedGraph { public: DirectedGraph(); // Tested - int getSize(); + 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 change_arc_weight(DirectedGraph*, void*, void*, float); + bool changeArcWeight(T, T, K); // Tested int connected_vertices_count(DirectedGraph*, void*); int _connected_vertices_count_recursive(DirectedGraph*, void*); void* source_vertex(DirectedGraph*); @@ -208,4 +208,10 @@ template void DirectedGraph::enumerateVertices() { 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); } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 060e049..c014bba 100644 --- a/main.cpp +++ b/main.cpp @@ -175,6 +175,10 @@ int main(int argc, char** argv) 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(); + /* // Creating edges between vertices and assigning weights. add_arc(digraph, a, b, 4); add_arc(digraph, a, e, 2); From cb8430cf0870609f0fdf22d4a2d4860704900f3e Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sat, 7 Oct 2023 21:16:42 -0400 Subject: [PATCH 27/32] Implemented and tested getArcWeight --- directed_graph.h | 8 +++++++- main.cpp | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/directed_graph.h b/directed_graph.h index 152c8e1..a6afd8d 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -32,7 +32,7 @@ template class DirectedGraph { void* source_vertex(DirectedGraph*); void setVisitedField(bool); void buildTree(DirectedGraph*, void*); - float get_arc_weight(DirectedGraph*, void*, void*); + K getArcWeight(T, T); // Tested void reset_parent_links(DirectedGraph*); int compareVertex(void*, void*); LinkedList* dijkstra(DirectedGraph*, void*, void*); @@ -214,4 +214,10 @@ template bool DirectedGraph:: changeArcWeight(T ori 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); } \ No newline at end of file diff --git a/main.cpp b/main.cpp index c014bba..1bc68c3 100644 --- a/main.cpp +++ b/main.cpp @@ -179,6 +179,12 @@ int main(int argc, char** argv) 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); + /* // Creating edges between vertices and assigning weights. add_arc(digraph, a, b, 4); add_arc(digraph, a, e, 2); From 85ce3b97f820a205ff86616a237c322117eef103 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sat, 7 Oct 2023 21:24:59 -0400 Subject: [PATCH 28/32] Getting Source Vertex function ready --- directed_graph.cpp | 2 +- directed_graph.h | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/directed_graph.cpp b/directed_graph.cpp index 62f723e..fc51561 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. */ diff --git a/directed_graph.h b/directed_graph.h index a6afd8d..8ea782b 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -29,7 +29,7 @@ template class DirectedGraph { bool changeArcWeight(T, T, K); // Tested int connected_vertices_count(DirectedGraph*, void*); int _connected_vertices_count_recursive(DirectedGraph*, void*); - void* source_vertex(DirectedGraph*); + Vertex* sourceVertex(); void setVisitedField(bool); void buildTree(DirectedGraph*, void*); K getArcWeight(T, T); // Tested @@ -105,8 +105,9 @@ 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); + // 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); } /* @@ -177,7 +178,6 @@ template bool DirectedGraph::addArc(T origin, T des * parameters. */ template bool DirectedGraph::removeArc(T origin, T destination) { - Vertex* start = getVertex(origin); Vertex* end = getVertex(destination); return start->removeArc(end); @@ -191,7 +191,6 @@ template void DirectedGraph::setVisitedField(bool // For each vertex in the Directed Graph's vertex list. for(int i = 0; i < this->vertexList; i++) { - // Retrieve the vertex struct at index i. Vertex* v = this->vertexList->get(i); v->setVisited(value); } @@ -199,11 +198,12 @@ template void DirectedGraph::setVisitedField(bool template void DirectedGraph::enumerateVertices() { - // 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); 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"; } @@ -216,8 +216,17 @@ template bool DirectedGraph:: changeArcWeight(T ori return start->changeWeight(end, weight); } -template K DirectedGraph:: getArcWeight(T origin, T destination){ +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 from which all vertices can be visited is returned. + * If no such vertex exists, then NULL is returned. + */ +template Vertex* DirectedGraph::sourceVertex(){ + } \ No newline at end of file From 2e25277e5fe9113d8ce309ea515d142c0cc948a1 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 8 Oct 2023 11:03:33 -0400 Subject: [PATCH 29/32] Implemented and tested vertex counting functions --- directed_graph.h | 49 +++++++++++++++++++++++++++++++++++++++++++----- main.cpp | 13 +++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 8ea782b..3f94960 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -27,8 +27,7 @@ template class DirectedGraph { bool addArc(T, T, K); // Tested bool removeArc(T, T); // Tested bool changeArcWeight(T, T, K); // Tested - int connected_vertices_count(DirectedGraph*, void*); - int _connected_vertices_count_recursive(DirectedGraph*, void*); + int connectedVerticesCount(T); // Tested Vertex* sourceVertex(); void setVisitedField(bool); void buildTree(DirectedGraph*, void*); @@ -51,8 +50,9 @@ template class DirectedGraph { LinkedList*>* vertexList; int valueSize; float** adjacencyMatrix; - Vertex* getVertex(T); - LinkedList*>* getVertices(); + Vertex* getVertex(T); // Tested + LinkedList*>* getVertices(); // Tested + int connectedVerticesCountHelper(Vertex*); // Tested }; @@ -190,7 +190,7 @@ template bool DirectedGraph::removeArc(T origin, T template void DirectedGraph::setVisitedField(bool value) { // For each vertex in the Directed Graph's vertex list. - for(int i = 0; i < this->vertexList; i++) { + for(int i = 0; i < this->vertexList->getSize(); i++) { Vertex* v = this->vertexList->get(i); v->setVisited(value); } @@ -229,4 +229,43 @@ template K DirectedGraph::getArcWeight(T origin, T */ template Vertex* DirectedGraph::sourceVertex(){ +} + +/* + * 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; } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 1bc68c3..f9547a7 100644 --- a/main.cpp +++ b/main.cpp @@ -185,6 +185,19 @@ int main(int argc, char** argv) 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); + + graph->addArc('d','a',83); + int fromE3 = graph->connectedVerticesCount('e'); + printf("Added ard from d to a, connected vertices from e (expected 4): %d\n", fromE3); + graph->enumerateVertices(); + /* // Creating edges between vertices and assigning weights. add_arc(digraph, a, b, 4); add_arc(digraph, a, e, 2); From 7cfb933b256ea78764d83575789670c85db7cd9e Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 8 Oct 2023 20:59:09 -0400 Subject: [PATCH 30/32] Implementing Source function --- directed_graph.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index 3f94960..3b38dc0 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -29,7 +29,7 @@ template class DirectedGraph { bool changeArcWeight(T, T, K); // Tested int connectedVerticesCount(T); // Tested Vertex* sourceVertex(); - void setVisitedField(bool); + void setVisitedField(bool); // Tested void buildTree(DirectedGraph*, void*); K getArcWeight(T, T); // Tested void reset_parent_links(DirectedGraph*); @@ -228,7 +228,16 @@ template K DirectedGraph::getArcWeight(T origin, T * 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; } /* @@ -241,7 +250,7 @@ template int DirectedGraph::connectedVerticesCount( Vertex* source = this->getVertex(origin); int count = 0; count += this->connectedVerticesCountHelper(source); - + this->setVisitedField(false); return count; } From eb8500d235ed8b8b3897eb715f72da9f943ee701 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 29 Oct 2023 13:11:50 -0400 Subject: [PATCH 31/32] Implementing smaller methods --- directed_graph.cpp | 2 +- directed_graph.h | 42 ++++++++++++++++++++++++++++++++++++------ main.cpp | 9 ++++++++- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/directed_graph.cpp b/directed_graph.cpp index fc51561..e2b8751 100644 --- a/directed_graph.cpp +++ b/directed_graph.cpp @@ -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. */ /* diff --git a/directed_graph.h b/directed_graph.h index 3b38dc0..b562bea 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -28,12 +28,12 @@ template class DirectedGraph { bool removeArc(T, T); // Tested bool changeArcWeight(T, T, K); // Tested int connectedVerticesCount(T); // Tested - Vertex* sourceVertex(); + Vertex* sourceVertex(); // Tested void setVisitedField(bool); // Tested void buildTree(DirectedGraph*, void*); K getArcWeight(T, T); // Tested - void reset_parent_links(DirectedGraph*); - int compareVertex(void*, void*); + void resetParentLinks(); // Implemented + 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*); @@ -42,7 +42,7 @@ template class DirectedGraph { DirectedGraph* create_digraph_from_file(char*); float extract_value(int, int, char*); float* float_arr_from_str(char*); - int value_count(char*); + int valueCount(char*); // Implemented //std::ostream& operator<<(std::ostream& o, DirectedGraph& net); void enumerateVertices(); @@ -224,7 +224,7 @@ template K DirectedGraph::getArcWeight(T origin, T /* * The sourceVertex function traverses the Directed Graph structure from each - * vertex. The first vertex from which all vertices can be visited is returned. + * 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(){ @@ -237,7 +237,7 @@ template Vertex* DirectedGraph::sourceVertex() } } - return; + return nullptr; } /* @@ -277,4 +277,34 @@ template int DirectedGraph::connectedVerticesCountH } 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; } \ No newline at end of file diff --git a/main.cpp b/main.cpp index f9547a7..15884b1 100644 --- a/main.cpp +++ b/main.cpp @@ -195,9 +195,16 @@ int main(int argc, char** argv) graph->addArc('d','a',83); int fromE3 = graph->connectedVerticesCount('e'); - printf("Added ard from d to a, connected vertices from e (expected 4): %d\n", fromE3); + printf("Added arc from d to a, connected vertices from e (expected 4): %d\n", fromE3); graph->enumerateVertices(); + graph->addArc('d','e',0); + printf("Added arc from d to e"); + graph->enumerateVertices(); + + Vertex* source = graph->sourceVertex(); + std::cout << "Computing source vertex (expected a): " << source->getData() << "\n"; + /* // Creating edges between vertices and assigning weights. add_arc(digraph, a, b, 4); add_arc(digraph, a, e, 2); From 2bfa751200d3f15ad8b8e16ce46dcf677e7076e7 Mon Sep 17 00:00:00 2001 From: "Michael St. Denis" Date: Sun, 29 Oct 2023 14:29:56 -0400 Subject: [PATCH 32/32] implemented buildTree, yet to test --- directed_graph.h | 56 ++++++++++++++++++++++++++++++++++++++++++++++-- vertex0.h | 3 +++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/directed_graph.h b/directed_graph.h index b562bea..c367d12 100644 --- a/directed_graph.h +++ b/directed_graph.h @@ -30,9 +30,8 @@ template class DirectedGraph { int connectedVerticesCount(T); // Tested Vertex* sourceVertex(); // Tested void setVisitedField(bool); // Tested - void buildTree(DirectedGraph*, void*); + void buildTree(Vertex*); K getArcWeight(T, T); // Tested - void resetParentLinks(); // Implemented 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*); @@ -53,6 +52,7 @@ template class DirectedGraph { Vertex* getVertex(T); // Tested LinkedList*>* getVertices(); // Tested int connectedVerticesCountHelper(Vertex*); // Tested + void resetParentLinks(); // Implemented }; @@ -307,4 +307,56 @@ template int DirectedGraph::valueCount(char* buffer 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/vertex0.h b/vertex0.h index 693b739..96ec17d 100644 --- a/vertex0.h +++ b/vertex0.h @@ -10,6 +10,9 @@ template class Vertex { 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.