Question

C++ In this project, you will be implementing a Graph data structure using a professional library....

C++

In this project, you will be implementing a Graph data structure using a professional library. You will construct a graph using the functions/methods it provides and then apply three common algorithms to process the data/information. Requirements Properly implement a graph data structure using existing graph libraries that includes at least 20 vertices and 20 edges. Utilize three different algorithms on your graph.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//using boost library to implement graph data structure

#include<boost/graph/adjacency_list.hpp>

#include <boost/graph/depth_first_search.hpp>

#include <iostream>

using namespace boost;

using namespace std;

typedef property<edge_weight_t, int> EdgeWeightProperty;

typedef boost::adjacency_list<listS,vecS,directedS,no_property,EdgeWeightProperty> Graph;

typedef Graph::edge_descriptor MyEdge;

class my_dfs_visitor : public boost::default_dfs_visitor

{

public: template < typename Vertex, typename Graph >

void discover_vertex(Vertex u, const Graph & g)

const { std::cout << "at " << u << std::endl; }

template < typename Edge, typename Graph >

void examine_edge(Edge e, const Graph& g)

const { std::cout << "Examining edges " << e << std::endl;

}

};

class my_bfs_visitor : public boost::default_bfs_visitor

{

public: template < typename Vertex, typename Graph >

void discover_vertex(Vertex u, const Graph & g)

const { std::cout << "at " << u << std::endl; }

template < typename Edge, typename Graph >

void examine_edge(Edge e, const Graph& g)

const { std::cout << "Examining edges " << e << std::endl;

}

};

int main()

{

Graph graph;

  

//creating 40 edges & 20 vertices using add_edge which accepts parameters as (source vertex, destination vertex, weight, graph object)

add_edge (0, 3, 8, graph);

add_edge (1, 4, 18, graph);

add_edge (2, 5, 28, graph);

add_edge (3, 6, 82, graph);

add_edge (4, 7, 81, graph);

add_edge (5, 8, 9, graph);

add_edge (6, 9, 7, graph);

add_edge (7, 18, 80, graph);

add_edge (8, 20, 7, graph);

add_edge (9, 10, 84, graph);

add_edge (10, 19, 48, graph);

add_edge (11, 10, 56, graph);

add_edge (12, 17, 53, graph);

add_edge (13, 17, 2, graph);

add_edge (14, 16, 1, graph);

add_edge (15, 17, 3, graph);

add_edge (16, 2, 4, graph);

add_edge (17, 5, 9, graph);

add_edge (18, 4, 7, graph);

add_edge (19, 3, 85, graph);

add_edge (20, 1, 11, graph);

add_edge (0, 20, 8, graph);

add_edge (1, 19, 18, graph);

add_edge (2, 18, 28, graph);

add_edge (3, 17, 82, graph);

add_edge (4, 16, 81, graph);

add_edge (5, 15, 9, graph);

add_edge (6, 14, 7, graph);

add_edge (7, 13, 80, graph);

add_edge (8, 12, 7, graph);

add_edge (9, 11, 84, graph);

add_edge (10, 11, 48, graph);

add_edge (11, 9, 56, graph);

add_edge (12, 8, 53, graph);

add_edge (13, 7, 2, graph);

add_edge (14, 6, 1, graph);

add_edge (15, 5, 3, graph);

add_edge (16, 4, 4, graph);

add_edge (17, 3, 9, graph);

add_edge (18, 2, 7, graph);

add_edge (19, 1, 85, graph);

add_edge (20, 3, 11, graph);

//1st Algorithm: Finding Minimum Spanning Tree using Kruskal Algorithm

std::list < MyEdge > spanningTree;

kruskal_minimum_spanning_tree(graph, std::back_inserter(spanningTree));

for(std::list < MyEdge >::iterator e = spanningTree.begin(); e != spanningTree.end(); ++e)

{

cout << *e << " ";

}

cout << "\n";

  

//2nd Algorithm: Depth First Search

my_dfs_visitor my_visitor;

depth_first_search(graph, visitor(my_visitor));

  

//3rd Algorithm: Breadth First Search

my_bfs_visitor my_visitor_2;

breadth_first_search(graph, visitor(my_visitor_2));

}

Add a comment
Know the answer?
Add Answer to:
C++ In this project, you will be implementing a Graph data structure using a professional library....
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
  • 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...

  • Below is the Graph file that needs to be modified(using Python3) : #!/usr/bin/python3 # Simple Vertex...

    Below is the Graph file that needs to be modified(using Python3) : #!/usr/bin/python3 # Simple Vertex class class Vertex: """ Lightweight vertex structure for a graph. Vertices can have the following labels: UNEXPLORED VISITED Assuming the element of a vertex is string type """ __slots__ = '_element', '_label' def __init__(self, element, label="UNEXPLORED"): """ Constructor. """ self._element = element self._label = label def element(self): """ Return element associated with this vertex. """ return self._element def getLabel(self): """ Get label assigned to...

  • Implement Huffman coding and Arithmetic coding algorithms using your favorite programming language. Note that you can't...

    Implement Huffman coding and Arithmetic coding algorithms using your favorite programming language. Note that you can't use existing libraries. Generate at least three types of statistically different artificial data sources to test your implementation of these algorithms (keep them short enough). Compare and comment on each algorithm’s performance in terms of the compression ratio for each type of data source. Please include: (i) the source code. (ii) your data sources. (iii) experiment results in your report. For the experiment results,...

  • [C++]Project #3 – Traversing Property Graphs EDIT: in reply to the comment, what excerpt are you...

    [C++]Project #3 – Traversing Property Graphs EDIT: in reply to the comment, what excerpt are you talking about? EDIT #2 : Need to construct a directed graph, where the property names label edges connecting nodes; either using array or linked list implementation. Learning Objectives Implement a data structure to meet given specifications Design, implement, and use a graph data structure Perform analysis of algorithm performance Utilize Dijkstra's algorithm Overview Your task for this assignment is to implement a graph data...

  • #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;...

  • C++ Data structure and algorithm Graph Algorithms Q2. (30 pts) Find the minimum spanning tree using...

    C++ Data structure and algorithm Graph Algorithms Q2. (30 pts) Find the minimum spanning tree using Prim's algorithm for the following graph. For this question, the solution must be provided step by step as shown in your textbook in Figures 9.51 in page 415. 6 FRL B 4

  • 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;...

  • v. 15 points) implementing data structure operations C++ code for a new public member function to...

    v. 15 points) implementing data structure operations C++ code for a new public member function to be added to the doubly linked list data structure used in class. Return the average value of all elements in the list ple a list of integers) Throws a length error exception if the list is empty. template «typename E> E& 0; average You may use loops or recursion as needed. If necessary, you may add private helper function0s) The definition of the DLinkedList...

  • Working on a program where my task is to implement a library (as a set of...

    Working on a program where my task is to implement a library (as a set of source and header files, documentation, and a Makefile) that provides a few useful functions for working with a counted string data structure. And one of the functions that needs to be written is described below but I am having a little difficulty writing it. Program needs to be written in C. Struct: typedef struct {    char *data;    size_t length; } kstring; function:...

  • Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the followi...

    Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the following classes Node.java: This class represents a vertex in the graph. It has only a single instance variable of type int which is set in the constructor. Implement hashCode() and equals(..) methods which are both based on the number instance variable Node - int number +Node(int number); +int getNumberO; +int hashCode() +boolean equals(Object o) +String toString0) Edge.java: This class represents a...

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