0% found this document useful (0 votes)
102 views

In Today's Lab We Will Design and Implement The Graph ADT

The driver file tests operations on a graph data structure including: finding the outdegree of vertices, performing depth-first and breadth-first searches to find paths between vertices, and printing the graph.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

In Today's Lab We Will Design and Implement The Graph ADT

The driver file tests operations on a graph data structure including: finding the outdegree of vertices, performing depth-first and breadth-first searches to find paths between vertices, and printing the graph.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSE225L – Data Structures a nd Alg o rithms Lab

Lab 15
Graph
In today’s lab we will design and implement the Graph ADT.

graphtype.h template<class VertexType>


#ifndef GRAPHTYPE_H_INCLUDED GraphType<VertexType>::~GraphType()
#define GRAPHTYPE_H_INCLUDED {
#include "stacktype.h" delete [] vertices;
#include "quetype.h" delete [] marks;
template<class VertexType> for(int i=0;i<maxVertices;i++)
class GraphType delete [] edges[i];
{ delete [] edges;
public: }
GraphType(); template<class VertexType>
GraphType(int maxV); void GraphType<VertexType>::MakeEmpty()
~GraphType(); {
void MakeEmpty(); numVertices = 0;
bool IsEmpty(); }
bool IsFull(); template<class VertexType>
void AddVertex(VertexType); bool GraphType<VertexType>::IsEmpty()
void AddEdge(VertexType, {
VertexType, int); return (numVertices == 0);
int WeightIs(VertexType, }
VertexType); template<class VertexType>
void GetToVertices(VertexType, bool GraphType<VertexType>::IsFull()
QueType<VertexType>&); {
void ClearMarks(); return (numVertices == maxVertices);
void MarkVertex(VertexType); }
bool IsMarked(VertexType); template<class VertexType>
void DepthFirstSearch(VertexType, void GraphType<VertexType>::AddVertex(VertexType
VertexType); vertex)
void BreadthFirstSearch(VertexType, {
VertexType); vertices[numVertices] = vertex;
private: for (int index=0; index<numVertices; index++)
int numVertices; {
int maxVertices; edges[numVertices][index] = NULL_EDGE;
VertexType* vertices; edges[index][numVertices] = NULL_EDGE;
int **edges; }
bool* marks; numVertices++;
}; }
#endif // GRAPHTYPE_H_INCLUDED template<class VertexType>
heaptype.cpp int IndexIs(VertexType* vertices, VertexType
#include "graphtype.h" vertex)
#include "stacktype.cpp" {
#include "quetype.cpp" int index = 0;
#include <iostream> while (!(vertex == vertices[index]))
using namespace std; index++;
const int NULL_EDGE = 0; return index;
}
template<class VertexType> template<class VertexType>
GraphType<VertexType>::GraphType() void GraphType<VertexType>::ClearMarks()
{ {
numVertices = 0; for(int i=0; i<maxVertices; i++)
maxVertices = 50; marks[i] = false;
vertices = new VertexType[50]; }
edges = new int*[50]; template<class VertexType>
for(int i=0;i<50;i++) void GraphType<VertexType>::MarkVertex(VertexType
edges[i] = new int [50]; vertex)
marks = new bool[50]; {
} int index = IndexIs(vertices, vertex);
template<class VertexType> marks[index] = true;
GraphType<VertexType>::GraphType(int maxV) }
{ template<class VertexType>
numVertices = 0; bool GraphType<VertexType>::IsMarked(VertexType
maxVertices = maxV; vertex)
vertices = new VertexType[maxV]; {
edges = new int*[maxV]; int index = IndexIs(vertices, vertex);
for(int i=0;i<maxV;i++) return marks[index];
edges[i] = new int [maxV]; }
marks = new bool[maxV];
}
template<class VertexType>
void GraphType<VertexType>::AddEdge(VertexType fromVertex, VertexType toVertex, int weight)
{
int row = IndexIs(vertices, fromVertex);
int col= IndexIs(vertices, toVertex);
edges[row][col] = weight;
}
template<class VertexType>
int GraphType<VertexType>::WeightIs(VertexType fromVertex, VertexType toVertex)
{
int row = IndexIs(vertices, fromVertex);
int col= IndexIs(vertices, toVertex);
return edges[row][col];
}
template<class VertexType>
void GraphType<VertexType>::GetToVertices(VertexType vertex, QueType<VertexType>& adjVertices)
{
int fromIndex, toIndex;
fromIndex = IndexIs(vertices, vertex);
for (toIndex = 0; toIndex < numVertices; toIndex++)
if (edges[fromIndex][toIndex] != NULL_EDGE)
adjVertices.Enqueue(vertices[toIndex]);
}
template<class VertexType> template<class VertexType>
void void
GraphType<VertexType>::DepthFirstSearch(Vertex GraphType<VertexType>::BreadthFirstSearch(Vertex
Type startVertex, VertexType endVertex) Type startVertex, VertexType endVertex)
{ {
StackType<VertexType> stack; QueType<VertexType> queue;
QueType<VertexType> vertexQ; QueType<VertexType> vertexQ;
bool found = false;
VertexType vertex, item; bool found = false;
VertexType vertex, item;
ClearMarks();
stack.Push(startVertex); ClearMarks();
do queue.Enqueue(startVertex);
{ do
vertex = stack.Top(); {
stack.Pop(); queue.Dequeue(vertex);
if (vertex == endVertex) if (vertex == endVertex)
{ {
cout << vertex << " "; cout << vertex << " ";
found = true; found = true;
} }
else else
{ {
if (!IsMarked(vertex)) if (!IsMarked(vertex))
{ {
MarkVertex(vertex); MarkVertex(vertex);
cout << vertex << " "; cout << vertex << " ";
GetToVertices(vertex,vertexQ); GetToVertices(vertex, vertexQ);
while (!vertexQ.IsEmpty())
{ while (!vertexQ.IsEmpty())
vertexQ.Dequeue(item); {
if (!IsMarked(item)) vertexQ.Dequeue(item);
stack.Push(item); if (!IsMarked(item))
} queue.Enqueue(item);
} }
} }
} while (!stack.IsEmpty() && !found); }
cout << endl; } while (!queue.IsEmpty() && !found);
if (!found) cout << endl;
cout << "Path not found." << endl; if (!found)
} cout << "Path not found." << endl;
}
Now generate the Driver file (main.cpp) where you perform the following tasks:

Operation to Be Tested and Description of Action Input Values Expected Output


• Generate the following graph. Assume that all edge costs are
1.
F

H G

E D

A
C

• Outdegree of a particular vertex in a graph is the number of


edges going out from that vertex to other vertices. For
instance the outdegree of vertex B in the above graph is 1.
Add a member function OutDegree to the GraphType
class which returns the outdegree of a given vertex.

int OutDegree(VertexType v);

• Add a member function to the class which determines if there


is an edge between two vertices.

bool FoundEdge(VertexType u, VertexType v);


• Print the outdegree of the vertex D. 3
• Print if there is an edge between vertices A and D. There is an edge.
• Print if there is an edge between vertices B and D. There is no edge.
• Use depth first search in order to find if there is a path from B BADGFHE
to E.

• Use depth first search in order to find if there is a path from E E


to B.
Path not found.
• Use breadth first search in order to find if there is a path from BACDE
B to E.
• Use breadth first search in order to find if there is a path from E
E to B.
Path not found.
• Modify the BreadthFirstSearch function so that it also
prints the length of the shortest path between two vertices.
• Determine the length of the shortest path from B to E. 3

You might also like