1
0

added more code

This commit is contained in:
Andrew W
2022-08-28 16:12:16 -05:00
parent 5a2894ed1b
commit 7dabaef6f6
2345 changed files with 1343530 additions and 0 deletions

164
CPE212/Project_06/graph.cpp Normal file
View File

@ -0,0 +1,164 @@
#include "graph.h"
// Graph
Graph::Graph()
// Constructor initializes vertices linked list to empty
{
vertices = NULL;
}
//~Graph()
Graph::~Graph()
// For each VertexNode in the vertices list, Destructor deallocates all EdgeNodes before
// deallocating the VertexNode itself
{
VertexNode* delV = vertices;
EdgeNode* delE = delV->edgePtr;
while (delV != NULL)
{
while (delE != NULL)
{
delE = NULL;
delE = delE->nextPtr;
}
delV = NULL;
delV = delV->nextVertex;
}
}
// Graph::AddVertex() - adds vertex name to vertices array; assumes space is available
void Graph::AddVertex(string v)
{
VertexNode* newVert = new VertexNode; // creates memeory for newVert
newVert->vname = v; // assigns the vertex name to v
newVert->edgePtr = NULL; // sets edgePtr to NULL
newVert->nextVertex = NULL; // Sets nextVertex to NULL
}
// Graph::AddEdge() - adds edge from source to destination vertex with specified weight
// assuming source and destination are already in vertices array
void Graph::AddEdge(string s, string d, int w)
{
VertexNode* Vertex_S = WhereIs(s);
VertexNode* Vertex_D = WhereIs(d);
// Initialize newEdge
EdgeNode* newEdge = new EdgeNode;
newEdge->destination = Vertex_D;
newEdge->weight = w;
newEdge->nextPtr = NULL;
if (Vertex_S->edgePtr == NULL)
{
Vertex_S->edgePtr = newEdge;
}
else
{
// Go the the end of edgePtr and add the newEdge to it
while (Vertex_S->edgePtr->nextPtr != NULL)
{
Vertex_S->edgePtr = Vertex_S->edgePtr->nextPtr;
}
Vertex_S->edgePtr->nextPtr = newEdge;
}
}
// Graph::IsPresent() - returns true if vertex in graph, false otherwise
bool Graph::IsPresent(string v)
{
VertexNode* vertPresent = vertices;
//Traverse vertices
while (vertPresent != NULL)
{
if (vertPresent->vname == v)
return true; // found
vertPresent = vertPresent->nextVertex; // advance through graph
}
// not found
return false;
}
// WhereIs() - returns pointer to specified vertex, throws exception if vertex not present
VertexNode* Graph::WhereIs(string v)
{
VertexNode* tempVertex = vertices;
// If found
while (tempVertex != NULL)
{
if (tempVertex->vname == v)
return tempVertex; // Found
tempVertex = tempVertex->nextVertex;
}
// If not found
throw GraphVertexNotFound();
}
// Graph::WeightIs() -- returns edge weight if it exists; otherwise, throws GraphEdgeNotFound
int Graph::WeightIs(string s, string d)
{
VertexNode* sourceV = WhereIs(s);
VertexNode* destV = WhereIs(d);
// search through edge nodes
while (sourceV->edgePtr != NULL)
{
if (sourceV->edgePtr->destination == destV)
{
return sourceV->edgePtr->weight;
}
sourceV->edgePtr = sourceV->edgePtr;
}
throw GraphEdgeNotFound();
}
void Graph::MarkVertex(string v)
{
// MarkVertex()
// Marks vertex V as visited
VertexNode* vistedV = WhereIs(v);
vistedV->mark = true;
}
// Graph::IsMarked() - returns mark status of vertex; assumes vertex is present
bool Graph::IsMarked(string v)
{
VertexNode* markedV = WhereIs(v);
return markedV->mark;
}
// Graph::GetToVertices() - returns a queue q of vertices adjacent to vertex named s
void Graph::GetToVertices(string s, queue<string>& q)
{
VertexNode* tempV = WhereIs(s);
tempV = tempV->nextVertex;
while (tempV != NULL)
{
q.push(tempV->vname);
tempV = tempV->nextVertex;
}
}
// DepthFirstSearch() - Finds path through graph from start vertex to destination vertex
void Graph::DepthFirstSearch(string startVertex, string endVertex, queue<string>& path)
{
stack<string> s;
queue<string> q;
if ( !( IsPresent(startVertex) && IsPresent(endVertex) ) )
throw GraphVertexNotFound();
bool found = false;
string vertex;
string item;
// more here if required
}

154
CPE212/Project_06/graph.h Normal file
View File

@ -0,0 +1,154 @@
//
// graph.h 2010 Fall CPE 212 - Project06 - Graphs and STL
//
// Specification file for Graph class
//
// DO NOT MODIFY OR SUBMIT THIS FILE
//
#ifndef GRAPH_H
#define GRAPH_H
#include <cstddef>
#include <new>
#include <iostream>
#include <iomanip>
#include <stack> // For STL stack
#include <queue> // For STL queue
#include <string>
using namespace std;
class GraphPathNotFound { }; // Exception class represents path-not-found condition
class GraphEdgeNotFound { }; // Exception class represents edge-not-found condition
class GraphVertexNotFound { }; // Exception class represents vertex-not-found condition
class GraphFull { }; // Exception class represents graph-full condition
struct VertexNode; // Forward declaration of VertexNode type
struct EdgeNode // Structure representing an edge
{
VertexNode* destination; // Pointer to destination vertex
int weight; // Edge weight
EdgeNode* nextPtr; // Pointer to next edge
};
struct VertexNode // Structure representing a vertex
{
string vname; // Name of vertex
bool mark; // Marked flag
EdgeNode* edgePtr; // Pointer to list of outgoing edges
VertexNode* nextVertex; // Pointer to next vertex in vertices list
};
class Graph // Graph ADT using adjacency list representation
{
private: //***** Private class members below *****//
VertexNode* vertices; // Linked list of vertex nodes
public: //***** Public members below *****//
Graph();
// Graph()
// Constructor initializes vertices linked list to empty
~Graph();
// ~Graph()
// For each VertexNode in the vertices list, Destructor deallocates all EdgeNodes before
// deallocating the VertexNode itself
void AddVertex(string v);
// AddVertex()
// Adds vertex to graph assuming vertex not already present
void AddEdge(string s, string d, int w);
// AddEdge()
// Adds edge from source S to destination D with specified weight W.
// If there is not enough memory to add the edge, throw the GraphFull exception
bool IsPresent(string v);
// IsPresent()
// Returns true if vertex V in graph, false otherwise
VertexNode* WhereIs(string v); /* Note: This function replaces IndexIs */
// WhereIs()
// Returns pointer to the vertex node that stores vertex v in the vertices linked list;
// Throws GraphVertexNotFound if V is not present in the vertices list
int WeightIs(string s, string d);
// WeightIs()
// Returns weight of edge (s,d). Throws GraphEdgeNotFound if edge not present.
void ClearMarks();
// ClearMarks()
// Clears all vertex marks
void MarkVertex(string v);
// MarkVertex()
// Marks vertex V as visited
bool IsMarked(string v);
// IsMarked()
// Returns true if vertex V is marked, false otherwise
void GetToVertices(string V, queue<string>& q);
// GetToVertices()
// Returns queue Q of vertex names of those vertices adjacent to vertex V
// The queue here is from the Standard Template Library
void DepthFirstSearch(string startVertex, string endVertex, queue<string>& path);
// DepthFirstSearch()
// Uses the DFS algorithm from the CPE 212 textbook to determine a path from the
// startVertex to the endVertex. If a path is found, the path vertices should
// be in the path queue. If no path is found, the path queue should be emptied
// as a signal to the client code that no path exists between the start and
// end vertices.
//
// Notes:
// (1) This algorithm is flawed in that as it searches for a path, it may
// output some additional vertices that it visited but were not part
// of the actual path. Implement the algorithm just as it appears in the textbook.
//
// (2) This algorithm requires use of the stack and queue containers from the
// Standard Template Library. The STL stack and queue interfaces may require
// minor modifications to the DFS code from the textbook.
// Print -- write graph to stdout. DO NOT MODIFY THIS FUNCTION!!!
void Print()
{
EdgeNode* eptr;
VertexNode* vptr = vertices;
const int FIELDWIDTH = 6;
string STARS = "**********";
STARS = STARS + STARS + STARS;
cout << endl << STARS << endl;
cout << setw(FIELDWIDTH) << "Vertex" << " : " << "Adjacent Vertices" << endl;
cout << "------------------------------" << endl;
while(vptr != NULL)
{
cout << setw(FIELDWIDTH) << vptr->vname << " : ";
eptr = vptr->edgePtr;
while (eptr != NULL)
{
cout << eptr->destination->vname << eptr->weight << " ";
eptr = eptr->nextPtr;
}
cout << endl;
vptr = vptr->nextVertex;
}
cout << STARS << endl << endl;
} // Graph::Print()
};
#endif

BIN
CPE212/Project_06/graph.o Normal file

Binary file not shown.

165
CPE212/Project_06/main.cpp Normal file
View File

@ -0,0 +1,165 @@
//
// main.cpp 2010 Fall CPE 212 - Project06 - Graphs and STL
//
// Driver program for Graph class -- The text files (read by this code) contain a series
// of commands that will help you test your Graph ADT code by triggering various class methods.
//
// DO NOT MODIFY OR SUBMIT THIS FILE
//
#include <iostream>
#include <fstream>
#include "graph.h"
#include <stack> // For STL stack class
#include <queue> // For STL queue class
using namespace std;
int main(int argc, char* argv[])
{
ifstream inputs; // Input file for commands
char op, ch; // Hold operation and optional char input
Graph* gPtr = NULL; // Will point to Graph object
int num; // Holds number of graph nodes
string v1, v2; // Vertex names input from file
int w; // Edge weight input from file
queue<string> path; // Computed path
string comment; // Comment input from file
// Output usage message if one input file name is not provided
if (argc != 2)
{
cout << "Usage:\n project07 <inputfile>\n";
return 1;
}
// Attempt to open input file -- terminate if file does not open
inputs.open(argv[1]);
if (!inputs)
{
cout << "Error - unable to open input file" << endl;
return 1;
}
cout << endl;
// Process commands from input file
getline(inputs, comment);
cout << comment << endl;
inputs >> op; // Attempt to input number of vertices
while (inputs)
{
switch (op)
{
case '#': // Comment
getline(inputs, comment);
cout << comment << endl;
break;
case 'c': // Add vertex
cout << "Constructor()" << endl;
gPtr = new Graph();
break;
case 'v': // Add vertex
inputs >> v1;
cout << "AddVertex(" << v1 << ")" << endl;
gPtr->AddVertex(v1);
break;
case 'd': // Add directed edge
inputs >> v1 >> v2 >> w;
cout << "AddEdge(" << v1 << ", " << v2 << ", " << w << ")" << endl;
gPtr->AddEdge(v1, v2, w);
break;
case 'u': // Add undirected edge
inputs >> v1 >> v2 >> w;
cout << "AddEdge(" << v1 << ", " << v2 << ", " << w << ")" << endl;
gPtr->AddEdge(v1, v2, w);
cout << "AddEdge(" << v2 << ", " << v1 << ", " << w << ")" << endl;
gPtr->AddEdge(v2, v1, w);
break;
case '?': // IsPresent()
inputs >> v1;
if (gPtr->IsPresent(v1))
cout << "IsPresent(" << v1 << ") -- true" << endl;
else
cout << "IsPresent(" << v1 << ") -- false" << endl;
break;
case 'w': // WeightIs()
inputs >> v1 >> v2;
try
{
cout << "WeightIs(" << v1 << ", " << v2 << ") -- ";
w = gPtr->WeightIs(v1, v2);
cout << w << endl;
}
catch ( GraphVertexNotFound )
{
cout << "Error: vertex not found" << endl;
}
catch ( GraphEdgeNotFound )
{
cout << "Error: edge not found" << endl;
}
break;
case 's': // Perform Depth-First Search
inputs >> v1 >> v2; // Input v1-start and v2-end vertices
cout << "DFS( " << v1 << ", " << v2 << " ) -- ";
try
{
gPtr->DepthFirstSearch(v1, v2, path);
if (path.empty())
cout << "No path found" << endl;
else
{
cout << " { ";
while (!path.empty())
{
cout << path.front() << " ";
path.pop();
}
cout << "}" << endl;
}
}
catch ( GraphVertexNotFound )
{
cout << "Error: vertex not found" << endl;
}
while (!path.empty())
path.pop();
break;
case 'p': // Print Graph
gPtr->Print();
break;
case '~': // Destructor
delete gPtr;
gPtr = NULL;
cout << "Destructor()" << endl << endl;
break;
default: // Error
cout << "Error - unrecognized operation '" << op << "'" << endl;
cout << "Terminating now..." << endl;
return 1;
break;
}
inputs >> op; // Attempt to input next command
}
return 0;
}

BIN
CPE212/Project_06/main.o Normal file

Binary file not shown.

View File

@ -0,0 +1,15 @@
# Project06 makefile
project06: graph.o main.o
g++ graph.o main.o -o project06
graph.o: graph.h graph.cpp
g++ -c graph.cpp
main.o: graph.h main.cpp
g++ -c main.cpp
clean:
rm *.o project06

View File

@ -0,0 +1,41 @@
# p06input1.txt - Test Graph(), ~Graph(), AddVertex(), AddEdge() with undirected graph
c
v A
v B
v C
v D
u A B 5
u A C 10
u B C 15
u B D 25
u C D 20
p
~
c
v A
v B
v C
v D
v E
v F
v G
v H
u A B 25
u A C 10
u A E 60
u B G 75
u B H 15
u C E 15
u D F 25
u D H 80
u E G 35
u E F 5
u F H 40
p
~

View File

@ -0,0 +1,40 @@
# p06input2.txt - Test Graph(), ~Graph(), AddVertex(), AddEdge() with directed graph
c
v A
v B
v C
v D
d A B 5
d A C 10
d B C 15
d B D 25
d C D 20
p
~
c
v A
v B
v C
v D
v E
v F
v G
v H
d A B 25
d A C 10
d A E 60
d B G 75
d B H 15
d C E 15
d D F 25
d D H 80
d E G 35
d E F 5
d F H 40
p
~

View File

@ -0,0 +1,33 @@
# p06input3.txt - Test IsPresent()
c
v A
v B
v C
v D
v E
v F
v G
v H
u A B 25
u A C 10
u A E 60
u B G 75
u B H 15
u C E 15
u D F 25
u D H 80
u E G 35
u E F 5
u F H 40
p
? A
? B
? G
? H
? K
? Z
~

View File

@ -0,0 +1,31 @@
# p06input4.txt - Test WeightIs() and its error handling
c
v A
v B
v C
v D
v E
v F
v G
v H
u A B 25
u A C 10
u A E 60
u B G 75
u B H 15
u C E 15
u D F 25
u D H 80
u E G 35
u E F 5
u F H 40
p
w E F
w F H
w A K
w A H
~

View File

@ -0,0 +1,23 @@
# p06input5.txt - Test DepthFirstSearch() and supporting functions
c
v A
v B
v C
v D
u A B 5
u A C 10
u B C 15
u B D 25
u C D 20
p
s A B
s A C
s A D
s A K
s D A
~

View File

@ -0,0 +1,21 @@
# p06input6.txt - Test DepthFirstSearch() and supporting functions
c
v A
v B
v C
v D
d A B 5
d A C 10
d B C 15
d D B 25
d C D 20
p
s A B
s A C
s A D
s A K
s D A
~

View File

@ -0,0 +1,34 @@
# p06input7.txt - Test DepthFirstSearch() and supporting functions
c
v A
v B
v C
v D
v E
v F
v G
v H
u A B 25
u A C 10
u A E 60
u B G 75
u B H 15
u C E 15
u D F 25
u D H 80
u E G 35
u E F 5
u F H 40
p
s A B
s A C
s A G
s A D
s A E
s A H
s A K
~

View File

@ -0,0 +1,36 @@
# p06input8.txt - Test DepthFirstSearch() and supporting functions
c
v A
v B
v C
v D
v E
v F
v G
v H
d A B 25
d A C 10
d A E 60
d B G 75
d B H 15
d C E 15
d D F 25
d D H 80
d E G 35
d E F 5
d F H 40
p
s A B
s A C
s A G
s A D
s A E
s A H
s A K
s G A
s H E
~

BIN
CPE212/Project_06/project06 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.