C :: Finding Number Of Connected Components From Undirected Disconnected Graph
Feb 17, 2013
How to find the number of connected components from an undirected disconnected graph.
the input I'm getting is like this:
7
1 2
4 5
3 6
2 7
the top number is the number of vertices and the rest of the numbers are the edges, eg. 1--2 is an edge.
Is there a way that you can implement DFS algorithm to find the number of connected components? like increment a variable every time DFS is called or something?
this is my first year programming, and in my class, each week we have to write a program. last week we wrote a program in c that made random value point and made a graph of the random points that continued on forever. this week, we have to use statistical functions to find the sum, mean, max, and min of the graph. below is the code i have so far.
I was asked to find the longest path in a graph. I was thinking about using Dijsktra's algorithm after multiplying all the weights with -1 and run the program in normal way and find the shortest path. And then I'll multiply with -1 again and get the longest path. I think this should give me the longest path, do you think it would work? And also, I'm dealing with considerably big data, such as 1.000.000 nodes and many more edges. etc. and I have a time limit of 2 seconds and memory limit of 128mb. Any other data structure instead of Adjacency Matrix? Because I'm pretty sure it will exceed the limits.
I've been trying to make a program to return node values for the shortest path from one node to another. I've searched up several algorithms like the Bellman Ford, A*, or Dijkstra and tried to think of ways to implement them if I store my map as a matrix. I've considered using a hash table, but since I am only a beginner, I am having trouble trying to understand how the concepts would translate into C.
Try not to make too much fun of me for my logic, but I'm having trouble with this. I am trying to make it so the program takes a 1 dimensional array and a 2 dimensional array, and checks to see what row in the 2 dimensional array is the closest to the 1D array.
To compute the value of the 1D array you take the first row first element in the 2D array, and the first element in the 1D array, subtract and the absolute value.
The whole temp part is kind of confusing myself. What I'm thinking is that I can add all row values up using
Code: tempRow += abs( x[i][j] - y[j] ); , then I need to compare that value to see if it is close to the "firEle" which is value I need to get closest to.
What I'm doing is giving the user the choice to show 2 different dates. 1 in yyyy-mm-dd notation and the other in dd-mm-yyyy notation.
I'm using a second form to do this.
So from Form1 I have my data in the datagrid and in Form2 I have 2 radio buttons giving the user the choice between Date1 and Date2. So how would I be able to interact with the first form with the radio buttons?
is supposed to initialize a 4-dimensional std::array a with a[1][2][3][2] = true.
The commented-out line, which is the desired recursion, does not compile for some reason, and the problem third parameter cannot be deduced. So I placed some temporary lines to work in the special case only. Howw to make that recursion work?
I have a small questions in the behavior of Structure, I have declare a structure with 10 components. Some of the components in the structure has already their value but when I use
The structure components in a function DLL function those value I set previously has been deleted or empty. I don't know what is the problem...
What is the most efficient algorithm for finding how many factors a number has? I've just been doing brute force division up to (n - 1) / 2 thus far. How can this be optimized?
I have to write a program to find the nth number of the Ulam numbers.
It's a bit complicated to explain what an Ulam number is but Wolfram explains it very well here: [URL]
I have to find the nth Ulam number but I don't know what I have to do to get that. My program gives me all the Ulam numbers from a range of 0 to n.
What I want the program to do is tell me that the 49th Ulam number is 243.
/* C++ Program to find nth Ulam Number */ #include <stdio.h> #include <iostream> #include <vector> using namespace std; int main() { int num = 0; vector<int> v;
after staring at this for awhile, I can't figure out why it won't work. It prints out numbers from 0 to 100, but will print out an absurdly high number as the highest number, like 790 or 640. I can't see why.
Code: #include <cstdlib> #include <ctime> #include <iostream> using namespace std; int find_highest(int array[]); int find_highest (int array[], int size) { int highest_num; for (int i = 1; i < size; ++i) { if (array[i] > array[i-1]) {
I'm supposed to find the biggest number (largest value), in any given array A with n numbers. (no floats)
My thoughts here are, check if A[0] > A[1]. if yes, check if A[0] > A[2]. If no, check if A[1] > A[2] etc.
I'm not sure how I can do this in code. I'm thinking if A[0] > A[1] is true, then set A[0] = A[k]. Kind of set it aside, and use that for the next if test. But I'm not sure how to do it.
This is my code so far.
Code: int main(){ int A[7] = {12, 6, 9, 33, 2, 25, 53}; int i, k; k = 0;
[Code]....
I'm aware of the flaws here, but this is the best I can do so far. How can I get the if test to use A[k] next, as A[k] will always be the biggest value?
I want the code to search for the first occurence of the word "character," and start counting the lines from that line until it hit the first occurrence of the word "story."
Right now, I am only getting a counter value of 1.
I was assigned to create a program that has a 10 x 8 two dimensional array and fill it with random numbers in the range 50 to 70 inclusively.( Got That part down). The second part is to make function named findSmallest that will find and print, appropriately labeled, the smallest number in the array.
I cant seem to get it working. The code for finding the smallest give me some weird number
Here my Code:
//Assignment 18 Program 2 #include <iostream> using namespace std;
I want to find third largest character in ascii as alphanumeritic. The problem is i cannot use arrays. How can I find third largest number ? Have to I compare all chars with each other ?
I need to calculate the expected time it takes to do an activity using a formula provided and after the loop is broken it needs to show the number of projects processed and the project with the longest expected time. I've got everything working except for finding the the project with the longest expected time. What I have here just keeps displaying the number of the last project processed regardless of if it was the largest or not, specifically lines 54-61 is what i can't seem to figure out.
#include <stdio.h> int main( void ) { unsigned int counter; int projectnumber; int optimistictime; int realistictime;
I'm trying to write a program where in you input 9 numbers from 1-10, then it determines the missing number. Here's my code. It has a lot of errors. I want to improve it by setting conditions like no repetition/ 0< number <10.
#include <iostream> using namespace std; int main() { int d[]={1,2,3,4,5,6,1,7,9, 10}; int i=0,j=0,c=0,missing=0; for (i = 0; i < 9; i++)
[Code] ....
I'm so new to C++. It always prints out the number 10 even though I typed it already. How to determine the missing one.
I'm trying to blur a ppm image by averaging the components of the color(r, g, b) within a certain reach of a specific pixel. This is a picture and description:
In this diagram, we are trying to compute the color for the pixel in the center (the red element). Its neighbors (within a reach of 4) are all of the green elements. The pixels outside of this 9x9 square are not considered in the blurring calculation for this pixel. To compute the color for the center pixel, average the red, green, and blue components (independently) of every pixel in the 9x9 square (including the pixel itself; the red element in this diagram).
I have some code, but it is not working correctly. The function "computed_color" on line 52 is shown in the next block of code.
Write a program that prompts the user to enter a number larger than 2. The program should use the Number class to determine the prime numbers between 2 and the number entered, and display them in the console window.I've been stuck for a while now and just lost in implementing classes and contstructors.
#include <iostream> using namespace std; int main(int argc, char * argv[]) { cout << "Enter a number larger than 2: " << endl; int n; cin >> n;
I am trying to find the largest prime factor of a number. But when I am trying to determine if a number is a prime number in the function:
int is_prime(int number), I am unable to exit the loop.
Here is the code:
#include<iostream> using namespace std; int is_prime(int number) //the problem is in this function { int num = number; int factor=0; do{ num++; for(int i=1;i<(num+1);i++){
[code].....
So when the program runs, it first divides 20 by 2, to get 10, then divides 10 by 2 to get 5. Since, // condition 1 is not met, it passes 2 to the function int is_prime(int number). The function is able to return 3, but cannot exit the loop when num is 4.
I think the problem lies in the function: int is_prime(int number).