Question

CSC 430              GRAPH PROJECT Implement a graph class using an adjacency matrix or an adjacency list....

CSC 430              GRAPH PROJECT

Implement a graph class using an adjacency matrix or an adjacency list.

The class should have a constructor, a copy constructor, a destructor and all the methods necessary to add/delete vertices, add/delete edges…

Write a menu-driven program to THOROUGHLY check your class and all the functions included in your program. You can choose the data type. Allow the user to continue with operations as long as he/she wants to.

Your program should check if an operation is possible ( ex: check if the graph is empty before attempting to delete an item; check if it is full before attempting to insert an item…)

Your main program should have the following functions:

  1. A DFS function to perform Depth First Search
  2. A BFS function to perform Breadth First Search
  3. A shortestPath functions that print the shortest path from a given vertex v to any other vertex
  4. Additional functions that will make your program more structured. C++ language
0 0
Add a comment Improve this question Transcribed image text
Answer #1

To write a C++ program to create a dfs and bfs tree and perform insertion and search operation on it.

ALGORITHM:

Step1: Start the program.

Step 2: Create a tree using linked representation with nodes having the structure LEFT->DATA->RIGHT. Build the tree in such a way that values at each left is less and values at each right is greater.

Step 3: Insert: Get the value to be inserted. Create a new node and set the data field to X. then set the left and right links to NULL.

Step 4: Compare X with root node data in X <root continue search in left subtree. If X=root, then display “Duplicate ”, if X>root then search in right subtree. Then insert the node in its right position.

Step 5: Display all the data in the tree.

Step 6: Stop the program.

PROGRAM:

#include<iostream>

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<stdlib.h>

#define NULL 0

using namespace std;

int cost[10 ][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10],stk[10],top,v,

main()

{

int m;

cout<<"enter the number of vertices";

cin>>n;

cout<<"enter no of edges";

cin>>m;

count<,"\nEDGES\n";

for(k=1;k<=m;k++)

{

cin >>i>>j;

cost[i][j]=1;

}

cout<<"enter the initial vertex';cin>>v;

cout<<"visited vertices\n";

cout<<v;

cout<<"DFS ORDER OF VISITED VERTICES:";

cout<< v <<" ";

visited[v]=1;

k=1;

while(k<n)

{

for(j=1;j<=n;j++)

if(cost[v][j]!=0 && visted[j]!=1 && visit[j]!=1

{

visit[j]=1;

stk[top]=j;

top++;

}

v=stk[--top];

cout<<v <<" ";k++;

visit[v]=0;

visited[v]=1;

}

qu[rare++]=j;

}

v=qu[front++];

count<<v << ";

k++;

visit[v]=0; visited[v]= 1;

}

}

struct search

{

int element;

struct search* left;

struct search *right;

};

struct search *t;

struct search *makeempty(struct search *);

struct search *findmin(struct search *);

struct search *findmax(struct search *);

void inorder(struct search *);

struct search *insert(int,struct search *);

struct search *delet(int,struct search *);

int retrieve(struct search *);

void main()

{

int choice,element;

clrscr();

printf(“\n\t\t tree”);

t=makeempty(NULL);

printf(“\n Operations on tree”);

printf("\n 1.Findmin \t 2.Findmax \t 3.Insert \t 4.Delete \t 5.Exit \n");

do

{

printf("\nEnter your choice:");

scanf("%d",&choice);

switch(choice)

{

case 1:

printf("\n Minimum:%d",retrieve(findmin(t)));

break;

case 2:

printf("\n Maximum:%d",retrieve(findmax(t)));

break;

case 3:

printf("\n Enter an element to insert:");

scanf("%d",&element);

t=insert(element,t);

inorder(t);

39

www.rejinpaul.combreak;

case 4:

printf("\nEnter the element to delete:");

scanf("%d",&element);

t=delet(element,t);

inorder(t);

break;

case 5:

exit(0);

}

}while(choice!=6);

getch();

}

struct search *makeempty(struct search *t)

{

if(t!=NULL)

{

makeempty(t->left);

makeempty(t->right);

free(t);

}

return(0);

}

struct search *findmin(struct search *t)

{

if(t==NULL)

return(NULL);

else if(t->left==NULL)

return(t);

else

return(findmin(t->left));

}

struct search *findmax(struct search *t)

{

if(t!=NULL)

while(t->right!=NULL)

t=t->right;

return(t);

}

struct search *insert(int x,struct search *t)

{

if(t==NULL)

{

40

www.rejinpaul.comt=(struct search *)malloc(sizeof(struct search));

if(t==NULL)

{

exit(1);

}

else

{

t->element=x;

t->left=t->right=NULL;

}

}

else

{

if(x<t->element)

t->left=insert(x,t->left);

else if(x>t->element)

t->right=insert(x,t->right);

}

return(t);

}

struct search *delete(int x, struct search *t)

{

if(t==NULL)

printf("\nElement not found");

else if(x<t->element)

t->left=delet(x,t->left);

else if(x>t->element)

t->right=delet(x,t->right);

else if(t->left && t->right)

{

tmp=findmin(t->right);

t->element=tmp->element;

t->right=delet(t->element,t->right);

}

else

{

tmp=t;

if(t->left==NULL)

t=t->right;

else if(t->right==NULL)

t=t->left;

free(tmp);

}

int retrieve(struct search *p)

{

41

www.rejinpaul.comreturn(p->element);

}

void inorder(struct search *t)

{

if(t!=NULL)

{

inorder(t->left);

printf("\t %d\t",t->element);

inorder(t->right);

}

}

getch();

return 0;

}

Add a comment
Know the answer?
Add Answer to:
CSC 430              GRAPH PROJECT Implement a graph class using an adjacency matrix or an adjacency list....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents th...

    JAVA LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents the graph. Your code should contain a function called addEdgelint i, int j). Use this function to add the appropriate edges in your matrix. Write code to implement Depth-First-Search (DFS) and Breadth-First-Search (BFS) of your graph. Let 0 be the source Node . Traverse the graph using DFS, print the Nodes as they are visited. . Traverse the...

  • from collections import defaultdict    # This class represents a directed graph using # adjacency list...

    from collections import defaultdict    # This class represents a directed graph using # adjacency list representation class Graph:        # Constructor     def __init__(self):            # default dictionary to store graph         self.graph = defaultdict(list)        # function to add an edge to graph     def addEdge(self,u,v):         self.graph[u].append(v)        # Function to print a BFS of graph     def BFS(self, s):            # Mark all the vertices as not visited         visited = [False] * (len(self.graph))            # Create a queue for BFS         queue...

  • 1. a. Using C++, represent the following graph using adjacency matrix, and implement DFS by using...

    1. a. Using C++, represent the following graph using adjacency matrix, and implement DFS by using stack (define it using class) to traverse the graph. b. Similarly, implement BFS (define queue using class) to traverse the graph c.When node 6 is printed, what numbers are in the stack (for DFS) and queue (for BFS) respectively? Draw pictures to show them. 1. a. Using C++, represent the following graph using adjacency matrix, and implement DFS by using stack (define it using...

  • Lab 11 Adjacency Matrix Graph Objective: Create a class which constructs an adjacency matrix representation of...

    Lab 11 Adjacency Matrix Graph Objective: Create a class which constructs an adjacency matrix representation of a graph and performs a few graph operations. Write an Adjacency Matrix Graph class which has the following: Two constructors: Default which makes the matrix of a pre-defined size Parameterized which takes in a non-negative or 0 size and creates an empty matrix addEdge: this method returns nothing and takes in two string parameters and a weight. The two integer parameters correspond to the...

  • #include <iostream> #include <queue> using namespace std; class Graph { public: Graph(int n); ~Graph(); void addEdge(int...

    #include <iostream> #include <queue> using namespace std; class Graph { public: Graph(int n); ~Graph(); void addEdge(int src, int tar); void BFTraversal(); void DFTraversal(); void printVertices(); void printEdges(); private: int vertexCount; int edgeCount; bool** adjMat; void BFS(int n, bool marked[]); void DFS(int n, bool marked[]); }; Graph::Graph(int n=0) { vertexCount = n; edgeCount = 0; if(n == 0) adjMat = 0; else { adjMat = new bool* [n]; for(int i=0; i < n; i++) adjMat[i] = new bool [n]; for(int i=0;...

  • /* Graph read from file, and represnted as adjacency list. To implement DFS and BFS on...

    /* Graph read from file, and represnted as adjacency list. To implement DFS and BFS on the graph */ #include <iostream> #include <sstream> #include <fstream> #include <vector> #include <utility> #include <unordered_map> #include <set> #include <queue> using namespace std; // Each vertex has an integer id. typedef vector<vector<pair<int,int>>> adjlist; // Pair: (head vertex, edge weight) adjlist makeGraph(ifstream& ifs); void printGraph(const adjlist& alist); vector<int> BFS(const adjlist& alist, int source); // Return vertices in BFS order vector<int> DFS(const adjlist& alist, int source); //...

  • 1. Startingatvertex000, perform a BFSof Q3.Assume all adjacency lists are in numericalorder.For example, (000,001) occurs before...

    1. Startingatvertex000, perform a BFSof Q3.Assume all adjacency lists are in numericalorder.For example, (000,001) occurs before (000, 010). Showthe resulting spanningtrees. Draw the directed graphs and perform a. 2. Breadth-First Search (BFS)algorithm: VTo determine the shortest paths starting at vertex a to everyother node. Show the resulting spanning tree. b. Depth-First Search (DFS) to explore the whole graph: Record the start/end time for all the vertices. show the resulting spanning forest Label the name°fthe edges. V Writethetopologicalorderofthevertices(ifnocycle-nobackedge) (Showthestate of the...

  • refer to the question using c++. if you could not do the bonus part no problem you don't have too , but if you can so please do it and let me know Create an unweighted undirected Graph Class usin...

    refer to the question using c++. if you could not do the bonus part no problem you don't have too , but if you can so please do it and let me know Create an unweighted undirected Graph Class using an Adjacency Matrix with the following functions 1. Graph(int numofV) 3. int noOfOutgoingEdges(int vertex); 4. int noOflncomingEdges (int vertex) 5. void print) You may use vectors/2D dynamic arrays to implement the matrix. Bonus (20) 6. void DFS(); Depth First Search...

  • Help with Java Program Please Create a simple graph class. The graph class should have the...

    Help with Java Program Please Create a simple graph class. The graph class should have the following items: an adjacency list or matrix to hold the graph data variables to hold the current size and max size of the graph default constructor create empty adjacency list/matrix max size = 10 overloaded constructor create empty adjacency list/matrix max size = int parameter isEmpty check if current size is zero createGraph read a formatted file and fill adjacency list/matrix The first line...

  • You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a...

    You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a graph stored as an adjacency list. The AdjacencyList class inherits from the Graph class shown below. class Graph { private: vector _distances; vector _previous; public: Graph() { } virtual int vertices() const = 0; virtual int edges() const = 0; virtual int distance(int) const = 0; virtual void bfs(int) const = 0; virtual void dfs(int) const = 0; virtual void display() const = 0;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT