Computer Science > QUESTIONS & ANSWERS > University of Colorado, Boulder - CSCI 2270Graph.cpp (All)
#include <iostream> #include "Graph.hpp" using namespace std; void Graph::addVertex(string name){ bool found = false; for(int i=0;i<vertices.size();i++){ if(vertices[i]->name == name){ found = ... true; } } if(found == false){ vertex* v = new vertex; v->name = name; vertices.push_back(v); } } void Graph::addEdge(std::string v1,std::string v2){ for(int i=0;i<vertices.size();i++){ if(vertices[i]->name == v1){ for(int j=0;j<vertices.size();j++){ if(vertices[j]->name == v2 && i != j){ adjVertex e0; e0.v = vertices[j]; vertices[i]->adj.push_back(e0); adjVertex e1; e1.v = vertices[i]; vertices[j]->adj.push_back(e1); } } } } } void Graph::displayEdges(){ for(int i=0;i<vertices.size();i++){ cout << vertices[i]->name << " --> "; for(int j=0;j<vertices[i]->adj.size();j++){ cout << vertices[i]->adj[j].v->name << " "; } cout << endl; } } void Graph::breadthFirstTraverse(string sourceVertex){ vertex* start = nullptr; for(int i=0;i<vertices.size();i++){ if(vertices[i]->name == sourceVertex){ start = vertices[i]; } } start->visited = true; start->distance = 0; queue<vertex*> q; q.push(start); cout << "Starting vertex (root): " << start->name << "-> "; while(!q.empty()){ vertex* n = q.front(); q.pop(); for(int i=0;i<n->adj.size();i++){ if(!n->adj[i].v->visited){ n->adj[i].v->visited = true; [Show More]
Last updated: 2 years ago
Preview 1 out of 2 pages
Buy this document to get the full access instantly
Instant Download Access after purchase
Buy NowInstant download
We Accept:
Can't find what you want? Try our AI powered Search
Connected school, study & course
About the document
Uploaded On
Apr 11, 2021
Number of pages
2
Written in
This document has been written for:
Uploaded
Apr 11, 2021
Downloads
0
Views
66
In Scholarfriends, a student can earn by offering help to other student. Students can help other students with materials by upploading their notes and earn money.
We're available through e-mail, Twitter, Facebook, and live chat.
FAQ
Questions? Leave a message!
Copyright © Scholarfriends · High quality services·