Question

In: Computer Science

Using C++: Create a function to search an undirected weighted graph and find the highest weighted...

Using C++:

Create a function to search an undirected weighted graph and find the highest weighted edge between two specific values.

This should include a class declaration and the definition for all required members that are needed to support the search. NO NEED to code those members. You can assume any other data structures such as stack, heap, or linked list is defined (so you can use their standard functions without declaring or defining them).

Solutions

Expert Solution

#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
#define n 6
class graph
{
private:
int g[n][n];
public:
int search()
{
int max=INT_MIN;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(g[i][j]>max)max=g[i][j];
}
}
return max;
}
void initialize()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
g[i][j]=0;
}
}
return ;
}
void addEdge(int x, int y,int w)
{
g[x][y]=w;
g[y][x]=w;
}
};

int main()
{
graph gra;
gra.initialize();
gra.addEdge(0,2,4);
gra.addEdge(3,2,2);
gra.addEdge(5,4,6);
gra.addEdge(1,4,9);
gra.addEdge(2,3,6);
gra.addEdge(5,3,7);
cout<<"The maximum weight of the edge is "<<gra.search();
return 0;
}

PLEASE DO UPVOTE IF THE ANSWER IS HELPFUL , AS IT GIVES CONFIDENCE TO HELP MORE STUDENTS


Related Solutions

give an example of a simple, undirected, weighted graph such that breadth-firstsearch outputs a search-tree that...
give an example of a simple, undirected, weighted graph such that breadth-firstsearch outputs a search-tree that is not a single source shortest path tree. Youranswer must (a) Specify the graphG= (V, E)by specifyingVandE. (b) Specify the weight functionw. (c) Specify an ordering of the vertices and the search-tree output by breadth-first search assuming the specified graph and ordering. (d) Specify a valid single-source shortest path treeT= (V, ET)by specifyingETand its root, the first vertex in your specified ordering. (e) Include...
Which graph search method will be better for finding a cycle in an undirected graph: a...
Which graph search method will be better for finding a cycle in an undirected graph: a BTS or a DFS? In detail, describe (create) an algorithm for finding a cycle in any undirected graph, explaining why the algorithm assures to find a cycle if there exists one.
Write a python code to generate a random undirected weighted graph and calculate the shortest distance...
Write a python code to generate a random undirected weighted graph and calculate the shortest distance from node i to node j (shortest path algorithm) where i is not equal to j. Store the results in text file. Thank you!
5. Suppose we are given both an undirected graph G with weighted edges and a minimum...
5. Suppose we are given both an undirected graph G with weighted edges and a minimum spanning tree T of G . (a) Describe an algorithm to update the minimum spanning tree when the weight of a single edge e is decreased. (b) Describe an algorithm to update the minimum spanning tree when the weight of a single edge e is increased. In both cases, the input to your algorithm is the edge e and its new weight; your algorithms...
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these...
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these structure emplate <typename T> class Tree {    struct TreeNode    {        T mData;        TreeNode* mLeft = nullptr;        TreeNode* mRight = nullptr;        TreeNode* mParent = nullptr;        bool mIsDead = false;        TreeNode()        {        }        TreeNode(T tData) : TreeNode()        {            mData = tData;...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Write a C program in Unix which uses a function called search to find the location...
Write a C program in Unix which uses a function called search to find the location of a value in THREE arrays of floats. The function should take three parameters :          the value to be found          the array to be searched          the size of the array N.B.!!!! The main program should read in three arrays of varying size          example : array a has twelve elements                    array b has six elements                    array c has nine...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence 8,3,10, 1,6,9, 14, 4,7, 13 with 8 as root of the tree. Find maximum and minimum elements of the tree, successor(10) and predecessor(13), print the inorder, postorder and preorder traversal of the tree.
Find the inventory values using (a) the weighted-average method, (b) the FIFO method, and (c) the...
Find the inventory values using (a) the weighted-average method, (b) the FIFO method, and (c) the LIFO method. Round to the nearest dollar. (15 pts) Purchases                                                    Beginning inventory: 100 units at $1.77 May:                            150 units at $2.79 August:                        250 units at $1.08 October:                      220 units at $2.60 Remaining in inventory: 540 units
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT