Dijkstra Algorithm not outputting correctly - c++

I am having trouble with implementing Dijkstra algorithm.
I have the following variables initialized :
enum GraphLimit300 {MAX_NODES = 50};
typedef unsigned int Element300;
Element300 adjacencyMatrix[MAX_NODES][MAX_NODES];
Element300 distanceArray[MAX_NODES];
bool visitedSet[MAX_NODES];
int numberNodes;
Here is my current implementation of the algorithm:
int startNode;
int visited;
unsigned int smallest = UINT_MAX;
do
{
startNode = getStartNode300();
setVisitedSet300();
smallest = UINT_MAX;
visitedSet[startNode] = true;
for (int i = 0; i < numberNodes; i++)
{
distanceArray[i] = adjacencyMatrix[startNode][i];
}
for (int i = 0; i < numberNodes - 1; i++)
{
for (int v = 0; v < numberNodes; v++)
{
if (visitedSet[v] == false)
{
if (distanceArray[v] < smallest)
{
smallest = distanceArray[v];
visited = v;
}
}
}
visitedSet[visited] = true;
for (int w = 0; w < numberNodes; w++)
{
if (visitedSet[w] == false)
{
distanceArray[w] = min(distanceArray[w], distanceArray[visited] + adjacencyMatrix[visited][w]);
}
}
}
On this particular for loop where it should do the certain math to find the min between values at a certain index where the nodes are false and store the min in that index. What I found is that after it chooses the smallest value in distaceArray and set it to true(which is 3 if I start at 1 using the data file below, Which is correct.).
for (int w = 0; w < numberNodes; w++)
{
if (visitedSet[w] == false)
{
distanceArray[w] = min(distanceArray[w], distanceArray[visited] + adjacencyMatrix[visited][w]);
}
}
It uses the nodes '0' and '2' as of this particular iteration is the nodes that are false. And does the math for times and stores them in the wrong array.
I am using this data set that stores the numbers in adjacencyMatrix correctly.
0 5 10 4294967295
4294967295 0 4294967295 3
4294967295 7 0 4294967295
4294967295 4294967295 4 0
The correct output is:
Distance[0] =4294967295
Distance[1] =0 //Which is the node that I choose to start with
Distance[2] =7
Distance[3] =3
What I am getting is:
Distance[0] =3
Distance[1] =0
Distance[2] =3
Distance[3] =3
I have done this process by hand to confirm that the correct output that I should be getting is true and it IS.
Updated if:
if (visitedSet[w] == false && adjacencyMatrix[visited][w] != UINT_MAX)
{
distanceArray[w] = min(distanceArray[w], distanceArray[visited] + adjacencyMatrix[visited][w]);
}

The problem here is again, the fact that you shall only consider edges which exist, that is adjacencyMatrix[visited][w]!=UINT_MAX.
If you don't exclude these edges, distanceArray[visited] + adjacencyMatrix[visited][w] will overflow and the min() will not return the result that you expect.
You can solve this by changing this line:
if (visitedSet[w] == false && adjacencyMatrix[visited][w]!=UINT_MAX )
Edit:
There is indeed another problem hidden in your nested for loops. The first inner for loop looks each time for the shortest subpath to expland. Unfortuantely, you didn't reset smallest, so that it starts with the smalest value of the previous iteration.
Update the looping as follows and you'll get your explected result:
for (int i = 0; i < numberNodes - 1; i++)
{
smallest = UINT_MAX; // !!!!!!!!!!!!!!!!!!!!!!!
for (int v = 0; v < numberNodes; v++)
{
...
}
...
}

Related

How would I write an algorithm to find the greatest jump from number to number in a sequence?

If your sequence is 4 2 1, the largest jump is from 4 to 2. If your sequence is 3 10 5 16 8 4 2 1, the largest jump is from 5 to 16.
I've made an algorithm however I'm not completely sure what I have done wrong (whever I haven't made the loop properly, set my variables correctly, or something else). I'm not sure what I need to set my index, BiggestDiff, or CurrentDiff too. I tried using a while loop to compare each number in my vector but I get zero (I'm assuming because I set BiggestDiff to zero)
If anyone can point me in the right direction, show me an example, or something else, that will be greatly appreciated.
Here is my code below
int findBiggestDiff(std::vector<int> sequence)
{
int index = 0;
int BiggestDiff = 0 ;
int CurrentDiff = BiggestDiff;
CurrentDiff = std::abs(sequence[index] - sequence[index + 1]);
while (index < sequence.size())
{
if (CurrentDiff > BiggestDiff)
{
BiggestDiff = CurrentDiff;
}
return index;
}
}
Try this:
{
int indexOfBiggestJump = 0;
int BiggestDiff = 0 ;
int CurrentDiff = BiggestDiff;
for(int i = 0; i < sequence.size() - 1; i++) {
CurrentDiff = std::abs(sequence[i] - sequence[i + 1]);
if (CurrentDiff > BiggestDiff)
{
BiggestDiff = CurrentDiff;
indexOfBiggestJump = i;
}
}
return indexOfBiggestJump;
}
There are several errors in your code.
your return index literally does nothing, only returns index (which will be 0) always.
you are not saving the index of the biggest jump anywhere.
if you are looking positions i and i + 1, you must go until sequence.size() - 1, otherwise you will look out of the bounds of sequence.
You aren't recalculating CurrentDiff at all. Also, your return statement in the in the wrong spot. You can do something like this (not tested)
int findLargest( const std::vector<int> &sequence ) {
if ( sequence.size() < 2 ) return -1; // if there's not at least two elements, there's nothing valid.
int index = 0;
int biggestIndex = -1;
int biggestDiff = -1;
while (index < sequence.size() - 1) // -1 so that the +1 below doesn't go out of range
{
// get the current difference
int currentDiff = std::abs(sequence[index] - sequence[index + 1]);
if (currentDiff > biggestDiff)
{
// update stats
biggestIndex = index;
biggestDiff = currentDiff;
}
++index;
}
return biggestIndex
}
int main() {
//…
int index = findLargest( sequence );
if ( index != -1 ) {
std::cout << "Biggest difference was between " << sequence[index] << " and " << sequence[index+1];
}
}

How to find local maximums in data set using C++?

I am using an arduino to read a sensor which stores 256 values into an array. I am trying to find local max's but some values being stored have repeating values to the left and right of itself causing the value to print multiple times. Is there a way to take all true values meaning they are a max value and store them in another array to process and reduce the repeated values to just 1 value...
OR is there a way to send the max values to another array where the repeated values get reduced to just 1? OR
IE:
Array1[] = {1,2,3,4,4,4,3,2,7,8,9,10}
max = 4 at index 3
max = 4 at index 4
max = 4 at index 5
since 4 is a peak point but repeats how can I reduce it so that the array looks like
Array2[] = {1,2,3,4,3,2,7,8,9,10}
max = 4 at index 3
I need the most basic breakdown if possible nothing on an expert level, thanks.
Code from Arduino:
int inp[20] = {24,100,13,155,154,157,156,140,14,175,158,102,169,160,190,100,200,164,143,20};
void setup()
{
Serial.begin(9600); // for debugging
}
void loop()
{
int i;
int count = 0;
for (i = 0; i < 20; i++)
{
Serial.println((String)inp[i]+" index at - "+i);
delay(100);
};
int N = 5; // loc max neighborhood size
for (int i = N-1; i < 19-N; i++)
{
bool loc = false;
for (int j = 1; j < N; j++) // look N-1 back and N-1 ahead
{
if (inp[i] > inp[i-j] && inp[i] > inp[i+j]) loc = true;
}
if (loc == true)
{
Serial.println((String)"max = "inp[i]+" at index "+i);
}
}
Serial.println("----------------------------------");
}
You can detect "local maxima" or peaks in a single loop without the need of copying something into another array. You just have to ignore repeating values, and you just have to keep track if the values considered are currently increasing or decreasing. Each value after which this status switches from increasing to decreasing is then a peak:
int main() {
int Array1[] = {1,2,3,4,4,4,3,2,7,8,9,10};
int prevVal = INT_MIN;
enum {
Ascending,
Descending
} direction = Ascending;
for (int i=0; i<sizeof(Array1)/sizeof(*Array1); i++) {
int curVal = Array1[i];
if (prevVal < curVal) { // (still) ascending?
direction = Ascending;
}
else if (prevVal > curVal) { // (still) descending?
if (direction != Descending) { // starts descending?
cout << "peak at index " << i-1 << ": " << prevVal << endl;
direction = Descending;
}
}
// prevVal == curVal is simply ignored...
prevVal = curVal;
}
}

Stuck in the following dp code

I wrote the following dp code for finding the prime factors of a number.
#include <bits/stdc++.h>
#define max 1000001
using namespace std;
vector <int> prime;
vector<bool> isprime(max,true);
vector<bool> visited(max,false);
vector<int> data(max,-1);
void dp(int n,int last)
{
if(n >= max || visited[n])
return;
visited[n] = true;
for(int i = last;i<prime.size();i++)
{
if(n*prime[i] >= max || data[n*prime[i]] != -1)
return;
data[n*prime[i]] = prime[i];
dp(n*prime[i],i);
}
}
int main()
{
isprime[1] = false;
data[1] = 1;
for(int i = 4;i<max;i += 2)
isprime[i] = false;
for(int i = 3; i*i< max;i += 2)
{
for(int j = i*i; j < max;j += i)
isprime[j] = false;
}
prime.push_back(2);
data[2] = 2;
for(int i =3;i<max;i += 2)
if(isprime[i])
{
prime.push_back(i);
data[i] = i;
}
for(int i = 0;i<prime.size();i++)
{
dp(prime[i],i);
}
cout<<"...1\n";
for(int i = 2;i<=8000;i++)
{
cout<<i<<" :- ";
int temp = i;
while(temp!= 1)
{
cout<<data[temp]<<" ";
temp = temp/data[temp];
}
cout<<endl;
}
return 0;
}
Here, last is the last index of prime number n.
But I am getting segmentation fault for this, when I change max to 10001, it runs perfectly. I'm not getting why is this happening since the data-structures used are 1-d vectors which can hold values up to 10^6 easily.
I checked your program out using GDB. The segfault is taking place at this line:
if(n*prime[i] >= max || data[n*prime[i]] != -1)
In your first ever call to DP in your for loop, where you call dp(2,0), the recursive calls eventually generate this call: dp(92692,2585).
92692 * 2585 = 239608820
This number is larger than a 32 bit integer can hold, so the r-value generated by the integer multiplication of those two numbers overflows and becomes negative. nprime[i] becomes negative, so your first condition of the above loop fails, and the second is checked. data[n * prime[i]] is accessed, and since n*prime[i] is negative, your program accesses invalid memory and segfaults. To fix this, simply change n to a long long in your parameter list and you should be fine.
void dp(long long n, int last)

Sieve of Eratosthenes - Removing non-prime from vector

I am trying to make this program work:
int main()
{
// prompt user for max number
// ...
// generate vector containing all numbers from 2 up to max
// ...
// give value 1 to not-prime numbers
// ...
// remove all not-prime numbers with value 1
// ...
// print out all numbers (all remaining are prime)
// ...
}
Everything works great, except the step where I need to recognize prime numbers and change their value to 1. I tried everything and I'm really out of ideas. Here is the last idea I had for that block of code:
// give value 1 to not-prime numbers
for (int i = 2; i < int(sqrt(max)); i++)
{
for (int j = 0; j < numbers.size(); j++)
{
numbers[numbers[j] * i - 2] = 1;
}
}
, and obviously it doesn't work. Help!
I assume number is a vector containing numbers from 2 to max (number[0] = 2)
Your loop should be :
// give value 1 to not-prime numbers
for (int i = 2; i < int(sqrt(max)); i++) // ok i are true numbers starting at 2
{
for (int j = (i * i) - 2; j < numbers.size(); j + = i) // j are indices for numbers starting at i * i (i*j for j< i already seen) stepping by i
{
numbers[j] = 1; // as j as indices do it directly
}
}
what could alse be written :
// give value 1 to not-prime numbers
for (int i = 2; i < int(sqrt(max)); i++) // ok i are true numbers starting at 2
{
for (int 0 = (i * i); j < max; j + = i) // j are true numbers starting at i * i (i*j for j< i already seen) stepping by i
{
numbers[j] = 1; // index for j is j-2
}
}
You should show more code in order for us to work with your tools. For instance, what is the type of numbers ? If you really are using C++, it is probably std::array or std::vector. I will assume you want to use an std::vector for convenience. Also, you should not use C-style cast like so : int(sqrt(max)).
So, here is what I would have done :
First, create a vector of booleans that has every indices from 0 to max. The index represent the number in the sieve, and the boolean value points out if this number is prime or not. To begin with, you need to assume that all numbers are prime, so you initialize your vector with true.
std::vector<bool> sieve(max+1, true); // Sets every value to true
I am including 0 and 1 in the sieve for convenience, but you can work with some index-2 everywhere in your code. Just to set things up, I need to put false here :
sieve[0] = sieve[1] = false;
Then, you want to proceed with the sieve algorithm.
int limit = static_cast<int>(sqrt(max));
for (int i = 2; i < limit; ++i)
{
// If i is a prime, proceed with multiples
if (sieve[i])
{
// Here, i*j is a multiple of i, hence not a prime (except when j = 1)
for (int j = 2; i*j <= max; ++j)
{
sieve[i*j] = false;
}
}
}
You can alternatively use this as the inner loop :
for (int j = 2*i; j <= max; j += i)
{
sieve[j] = false;
}
So now, your sieve is completed : every index with a corresponding true value is a prime, you only need to print them ! You can either copy them into another vector and then print out this last vector, of directly print the indices corresponding to a prime.

Finding Mode of Vector of Ints in C++

So I'm trying to make a basic program to learn the basics of C++, I'm generating 100 random numbers from 0 to 100 and storing them in a vector, I am then displaying the sum, mean, median, mode, high and low of the vector. I have everything else done except the mode which is where I get stuck. Here is the code I have so far.
int modeFunction()
{
numMode = 0;
count = 0;
for (int n = 0; n < 100; n++)
{
for (int y = 0; y < 100; y++)
{
if (numVector.at(y) == numVector.at(n))
{
numMode = numVector.at(y);
count++;
}
}
}
return numMode;
}
After that I get stuck because in my mind that should work but it doesn't. It just out puts the last number, usually 100. Any help would be much appreciated.
since all the values are between 0 and 100, you can find the mode efficiently with a histogram:
std::vector<int> histogram(101,0);
for( int i=0; i<100; ++i )
++histogram[ numVector[i] ];
return std::max_element( histogram.begin(), histogram.end() ) - histogram.begin();
Since mode is the number that occurs most frequent you shouldn't change numMode unless the new number's count is greater than numMode's count.
EDIT: To clarify, you need to keep a separate count for the current element and the current number that you think is the mode. Ideally, setting newMode to the first element is a good approach.
In addition, mode isn't necessary unique (i.e. "1 1 2 2"). You may want to keep that in mind if you care about that.
newMode = element[0]
modeCount = # of occurrence of newMode
for ( i-th element from [1 to end] ) {
tmpCount = # of occurrence of element[i]
if tmpCount > modeCount {
newMode = element[i]
modeCount = tmpCount
}
}
bmcnett's approach works great if number of elements are small enough. If you have large number of elements but the all element value are with in a small range using map/hashmap works well. Something like
typedef std::pair<int, int> mode_pair;
struct mode_predicate
{
bool operator()(mode_pair const& lhs, mode_pair const& rhs)
{
return lhs.second < rhs.second;
}
};
int modeFunction()
{
std::map<int, int> mode_map;
for (int n = 0; n < 100; n++)
mode_map[numVector[n]]++;
mode_predicate mp;
return std::max_element(mode_map.begin(), mode_map.end(), mp)->first;
}
Your algorithm is wrong - it outputs the last number in the array because that's all it can ever do. Every time the number at index y matches the number at index n you overwrite the results for the previous n. Since you're using the same loop conditions, y and n are always the same at at least one point in the nested loop for each possible n value - and you'll always end up with numMode being numVector.at(99).
You need to change your algorithm to save the count for each n index along the way (or at least which n index ended up with the greatest count), so that you can know at the end of the n loop which entry occured the most times.
Alternative solutions. Note: untested.
int mode1(const std::vector<int>& values)
{
int old_mode = 0;
int old_count = 0;
for(size_t n=0; n < values.size(); ++n)
{
int mode = values[n];
int count = std::count(values.begin()+n+1, values.end(), mode);
if(count > old_count)
{
old_mode = mode;
old_count = count;
}
}
return old_mode;
}
int mode2(const std::vector<int>& values)
{
return std::max_element(values.begin(), values.end(), [](int value)
{
return std::count(values.begin(), values.end(), value);
});
}
Mode means a number with highest frequency. The logic should be -
//Start of function
int mode = 0, globalCount = 0 ;
// Start of outer for loop
for i = 0 to length - 1
int localCount = 0
// Start of inner for loop
for j = 0 to length - 1
if vec[i] == vec[j]
++localCount
// End of Inner for loop
if ( localCount > globalCount )
globalCount = localCount
mode = vec[i]
// End of outer for loop
if globalCount > 1 // This should be checked whether vec has repetitions at all
return mode
else
return 0
// End of function
int number = array_list[0];
int mode = number;
int count = 1;
int countMode = 1;
for (int i=1; i<size_of_list; i++)
{
if (array_list[i] == number)
{ // count occurrences of the current number
count++;
if (count > countMode)
{
countMode = count; // mode is the biggest ocurrences
mode = number;
}
}
else
{ // now this is a different number
if (count > countMode)
{
countMode = count; // mode is the biggest ocurrences
mode = number;
}
count = 1; // reset count for the new number
number = array_list[i];
}
}