Question: Programming Language is C Given a graph we want to traverse all the nodes and compute the sum of the values of the nodes of

Programming Language is C
 Programming Language is C Given a graph we want to traverse

Given a graph we want to traverse all the nodes and compute the sum of the values of the nodes of the graph. You have to define a struct called Node that can hold pointers to its neighbors as arrays of same type pointers (assume at most 10 neighbors). For example (you may use another struct if you find it more useful): Struct Node int value; Node neighbors(10]: Int num_neighbors;/" actual number of valid neighbors in the neighbors array 7 Int index;/ index of the current node that can be used to find its location in arrays / Write a function that given a Node , recursively visits neighbors of the node and sum the values. Note that by doing it in this way, we end up going to first neighbor and then the first neighbor of the first neighbor and so on. Use an array of size-number of nodes to keep track which nodes you have seen already so that you don't use them in summation multiple times and also when you are done with one connected component you can find the next node to explore the next component, by finding the first element of the array that is zero (zero-not seen) Now use the function to print the sum of the values of each connected component of the graph given as input to the program. The graph has N nodes and M edges. Nodes are numbered from O to n-1. An edge is represented as 2 numbers between 0 and n-1 for the nodes at the two ends of that edge. The graph might not be completely connected but it may have multiple connected components. You have to compute the sum for each component and print all of them (each in a newline). The input is formatted as follows. N and M appear in first line of input (separated by space). The second line has N numbers separated with space, corresponding to the values of node O to n-1. Then M lines follow each with two numbers (space separated) that represent an edge (bidirectional, so if"a b" is given you also have to point b to a in addition to a to b) between them N M vo v1.. VN EOa E0b Ela E1b EMa EMb For instance: Input: 4 2 10 20 30 40 01 1 2 Output: 60 40

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!