In this assignment, you will implement a graph ADT (Abstract Data Type) with several functions. A graph G considered in this assignment consists of a set V of vertices and a set E of edges, where each vertex in V is a point on a Cartesian plane, and each edge is a line segment between the two points in V. Consequently, G is an undirected graph. Each point has a x-coordinate and a y-coordinate. We assume that the x-coordinate and the y-coordinate of each point are integers.
The distance of between two points v1=(x1, y1) and v2=(x2, y2) is square root of (x1 - x2)2 + (y1 - y2)2. The length of an edge is the distance between its two end points. Therefore, each edge has an implicit weight which is its edge length.
Given a path in a graph, its path length is the sum of lengths of all its edges. A path between two vertices is a shortest path if it has the minimum path length among all the paths between the two vertices.
Given two vertices u and v in a graph, u is reachable from v if there is a path from u to v.
Basic types are provided as follows:
// A vertex is a 2D point typedef struct Vertex { int x; // x-coordinate int y; // y-coordinate } Vertex;
// each edge is a pair of vertices (end-points) typedef struct Edge { Vertex *p1; // first end point Vertex *p2; // second end point } Edge;
// A vertex node stores a vertex and other information, and you need to expand this type typedef struct VertexNode { Vertex *v; } VertexNode;
typedef struct GraphRep { // graph header VertexNode *vertices; // an array of vertices or a linked list of vertices int nV; // #vertices int nE; // #edges } GraphRep;
typedef struct GraphRep *Graph;
The above types serve as a starting point only. You can revise them and add more types.
You need to implement the following functions:
Graph CreateEmptyGraph(). This function creates an empty graph and returns it.
int InsertEdge(Graph g, Edge *e). This function does the following task. Check if the edge e is in the graph g. If e is not in g, insert e into g and return 1. Otherwise, return 0.
void DeleteEdge(Graph g, Edge *e). This function deletes the edge e from the graph g. If e is not in g, it does nothing. After deleting the edge e, this function also deletes any isolated end vertex of e. An isolated vertex is a vertex without any edge between this vertex and any other vertex.
void ReachableVertices(Graph g, Vertex *v). This function finds all the vertices reachable from the vertex v in g and prints them in any order on the screen. A vertex u is reachable from a vertex v if there is a path from v to u. In the output, each vertex is displayed as a pair (x', y), where x and y are its x-coordinate and y-coordinate, and two adjacent vertices are separated by a comma (,). If no vertex is reachable from v, nothing will be printed. If v is not a vertex of g, this function does nothing.
void ShortestPath(Graph g, Vertex *u, Vertex *v). This function finds the shortest path between the vertex u and the vertex v, and print all the vertices of the shortest path in their order on the shortest path from the vertex u to the vertex v in the form of (x1, y1), , (x2, y2), where the first and second element of each pair are the x-coordinate and y-coordinate of the corresponding vertex. If either u or v is not a vertex of g, this function does nothing.
void FreeGraph(Graph g). This function frees the heap space occupied by the graph g.
void ShowGraph(Graph g). This function prints each edge of g once in breadth-first order. Your breadth-first search algorithm can pick any vertex as the first vertex to be visited in the breadth-first search. In the output, each vertex is displayed as a pair (x', y), where x and y are its x-coordinate and y-coordinate, and each edge (x1,y1)-(x2,y2) is displayed as (x1,y1),(x2,y2), and two adjacent edges are separated by a white space.
Time complexity analysis
You need to include the time complexity analysis of each function as comments in your program. Try your best to make each function time-efficient. Any time complexity that goes against the best algorithm you have learned in this course will receive some penalty. For example, the time complexity of your ShortestPath() function should not be higher than O((m+n)log n), where m and n are the number of edges and the number of vertices of graph g. There is no specific requirement on the space complexity. However, you need to make your functions as space efficient as possible.
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.