Tarjan algorithm based on BFS - breadth-first-search

I have an implemented BFS algorithm and I want to find SCC with Tarjan's algorithm using my BFS (implemented with colors and time method). Here is the code: /////////////////////////////////////////////////////////////
void dfs_visit_color(int v, Graf *G, int *time)
{
time++;
G->d[v] = *time;
G->color[v] = GRI;
NodeT *p = G->t[v];
int w;
printf("%d ", v);
while (p != NULL)
{
w = p->val;
if (G->color[w] == ALB)
{
T[w] = v;
dfs_visit_color(w, G, time);
}
p = p->next;
}
G->color[v] = NEGRU;
time++;
G->f[v] = *time;
}
void dfs_color(Graf *G){
int time = 0, i;
for (i = 0; i < G->n; i++)
{
if (G->color[i] = ALB);
}
for (i = 0; i < G->n; i++)
{
if (G->color[i] == ALB)
{
dfs_visit_color(i, G, &time);
}
}
}

Related

Quick sort throws a 'not in inclusive range' error but sometimes it works. (Dart)

I have implemented a quick sort algorithm in Dart which sorts a list of random integers in the range from 0 to 100. Most of the times, it throws a 'not in inclusive range' exception. Other times, it works. I don't understand what is wrong with my code or logic here.
import 'dart:math';
List<int> list = [];
void main() {
randomize();
quickSort(0, list.length - 1);
print(list);
}
void randomize() {
for (int i = 0; i < 100; ++i) {
list.add(Random().nextInt(100));
}
}
void quickSort(int l, int h) {
if (l < h) {
int mid = partition(l, h);
quickSort(l, mid - 1);
quickSort(mid + 1, h);
}
}
int partition(int l, int h) {
int pivot = l;
int i = l;
int j = h;
while (i < j) {
while (list[i] <= list[pivot]) {
++i;
}
while (list[j] > list[pivot]) {
--j;
}
if (i < j) {
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
int temp = list[pivot];
list[pivot] = list[j];
list[j] = temp;
return j;
}

Why am I getting wrong answer for this SPOJ Question

I still dont know weather I am allowed discuss Competetive Programming Doubts here, please let me know..
Can someone help me on this SPOJ question, I am getting wrong Answer:
https://www.spoj.com/problems/SPIKES/
I have tried all the test cases I could think of and the code always gives correct output.
Before posting it here, I also asked it in spoj forum but Its been two days, no one replies...
#include<bits/stdc++.h>
using namespace std;
// # = 35
// # = 64
// . = 46
// s = 115
// Using Dijkstra to find the path with minimum no. of Spikes
int n,m,j;
const int N = 100;
const int INF = 9;
vector<int> g[N];
int vis[N][N];
pair<int,int> dist[N][N];
vector<pair<int,int>> movements = {
{0,1},{0,-1},{1,0},{-1,0}
};
bool isvalid(int i, int j){
return i>=0 && j>=0 && i<n && j<m;
}
void bfs(int sourcei, int sourcej){
set<pair<int,pair<int,int>>> q;
q.insert({0,{sourcei,sourcej}});
dist[sourcei][sourcej] = {0,INF};
while(q.size()>0){
auto curr_v = *q.begin();
int curr_i = (curr_v.second).first;
int curr_j = (curr_v.second).second;
int spikes = curr_v.first;
q.erase(q.begin());
if(vis[curr_i][curr_j]) continue;
vis[curr_i][curr_j] = 1;
for(auto m : movements){
int child_i = curr_i + m.first;
int child_j = curr_j + m.second;
int spikec = spikes;
if(!isvalid(child_i,child_j)) continue;
if(g[child_i][child_j] == 115) spikec = spikes+1;
if(vis[child_i][child_j]) continue;
if(g[child_i][child_j]==35) continue;
if(dist[child_i][child_j].second > spikec){
dist[child_i][child_j] = {(dist[curr_i][curr_j].first +1),spikec};
q.insert({spikec , {child_i,child_j}});
}
}
}
}
int main(){
cin>>n>>m>>j;
int start_j,start_i;
int end_j,end_i;
for (int i = 0; i < N; ++i){
for (int j = 0; j < N; ++j){
dist[i][j].second = INF;
dist[i][j].first = 0;
}
}
for (int i = 0; i < n; ++i){
for (int j = 0; j < m; ++j){
char x; cin>>x;
g[i].push_back((int)x);
if(x=='#') {
start_i = i;
start_j = j;
}
if(x=='x'){
end_i = i;
end_j = j;
}
}
}
bfs(start_i,start_j);
// for (int i = 0; i < n; ++i){
// for (int j = 0; j < m; ++j){
// cout<<dist[i][j].first<<","<<dist[i][j].second<<" ";
// }cout<<endl;
// }
if(dist[end_i][end_j].second <= (int)j/2) cout<<"SUCCESS"<<endl;
else cout<<"IMPOSSIBLE"<<endl;
return 0;
}

add function is not working properly in Polynomial using linked list

I can't find any error in this code, please can anyone help me out with this.
please provide the whole code by debugging the given code.
I'm having a problem in adding two polynomials otherwise everything is fine.
I wasted my whole week in figuring out this bug but can't be able to find it.
so I will be happy if anyone can help me, please
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Node
{
int coeff;
int exp;
struct Node *next;
} *poly = NULL, *poly2 = NULL;
void create()
{
struct Node *t, *last = NULL;
int num, i;
printf("Enter number of terms");
scanf("%d", &num);
printf("Enter each term with coeff and exp\n");
for (i = 0; i < num; i++)
{
t = (struct Node *)malloc(sizeof(struct Node));
scanf("%d%d", &t->coeff, &t->exp);
t->next = NULL;
if (poly == NULL)
{
poly = last = t;
}
else
{
last->next = t;
last = t;
}
}
}
void create2()
{
struct Node *t, *last = NULL;
int num, i;
printf("Enter number of terms");
scanf("%d", &num);
printf("Enter each term with coeff and exp\n");
for (i = 0; i < num; i++)
{
t = (struct Node *)malloc(sizeof(struct Node));
scanf("%d%d", &t->coeff, &t->exp);
t->next = NULL;
if (poly2 == NULL)
{
poly2 = last = t;
}
else
{
last->next = t;
last = t;
}
}
}
void Display(struct Node *p)
{
while (p)
{
printf("%dx%d +", p->coeff, p->exp);
p = p->next;
}
printf("\n");
}
long Eval(struct Node *p, int x)
{
long val = 0;
while (p)
{
val += p->coeff * pow(x, p->exp);
p = p->next;
}
return val;
}
void add(struct Node *p, struct Node *q)
{
struct Node *sum = (struct Node *)malloc(sizeof(struct Node));
while (p && q)
{
if (p->exp > q->exp)
{
sum->exp = p->exp;
sum->coeff = p->coeff;
p = p->next;
if (p)
{
sum->next = (struct Node *)malloc(sizeof(struct Node));
sum = sum->next;
}
}
else if (p->exp < q->exp)
{
sum->exp = q->exp;
sum->coeff = q->coeff;
q = q->next;
if (q)
{
sum->next = (struct Node *)malloc(sizeof(struct Node));
sum = sum->next;
}
}
else
{
sum->exp = p->exp;
sum->coeff = p->coeff + q->coeff;
q = q->next;
p = p->next;
if (p && q)
{
sum->next = (struct Node *)malloc(sizeof(struct Node));
sum = sum->next;
}
}
}
while (p)
{
sum->next = (struct Node *)malloc(sizeof(struct Node));
sum = sum->next;
sum->exp = p->exp;
sum->coeff = p->coeff;
p = p->next;
}
while (q)
{
sum->next = (struct Node *)malloc(sizeof(struct Node));
sum = sum->next;
sum->exp = q->exp;
sum->coeff = q->coeff;
q = q->next;
}
sum->next = NULL;
Display(sum);
}
int main()
{
create();
create2();
Display(poly);
//printf("\n");
Display(poly2);
//printf("\n");
add(poly, poly2);
return 0;
}

stack smashing in C code about making a histogram

I need to make a c program that will make a histogram of all the letters present in a phrase the user gives. When I run it, I does it but gives a "* stack smashing detected *: terminated". Where would this error be coming from? (for ease right now I set max to 3). In the future i'll have it find the max
Thank you
Andrew
#include <stdio.h>
#include <ctype.h>
#include <string.h>
static void ReadText(int histo[26],int max) {
char phrase[100];
int i;
char Letter;
char toArray;
// read in phrase
printf("Enter Phrase: "); // reads in phrase with spaces between words
scanf("%[^\n]",phrase);
// count the number of certain letters that occur
for(i = 0; i <= strlen(phrase);++i) {
Letter = phrase[i];
if(isalpha(Letter) != 0){
Letter = tolower(Letter);
toArray = Letter - 97;
histo[(int)toArray] = histo[(int)toArray] + 1;
}
}
}
static void DrawHist(int histo[26], int max){
int i;
int j;
int histo2[50];
for(i = 0; i <= 26; i++) {
histo2[i+i] = histo[i];
if(i < 25) {
histo2[i+i+1] = 0;
}
}
// (i = 1; i <= 50; i++) {
// printf("%d",histo2[i]);
//}
//printf("\n");
for(i=max;i>0;--i) {
for(j=0;j<=51;++j) {
if((j < 51) && (histo2[j] >= i)) {
printf("|");
}
else if((j < 51) && (histo2[j] < i)){
printf(" ");
}
else if(j == 51){
printf("\n");
}
}
}
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
printf("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z\n");
}
int main() {
int histo[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int max = 3;
//int i;
ReadText(histo,max);
//for(i = 0; i<26;++i) {
// printf("%d",histo[i]);
//}
DrawHist(histo,max);
return 0;
}

How to separate two Low[1] by if statement?

Example, previous low is 1.55. When next candle low is 1.66, then next next candle low is 1.44 which is lower than previous low (1.55) then do something. How to achieve it?
double previous_low = 0;
double new_low = 0;
double new_low2 = 0;
if (((previous_low==0)&&(Low[1] < Low[2])){
previous_low = Low[1];
}
if (previous_low != 0){
if (Low[1] < previous_low){
new_low2 = Low[1]; //it fail
}
if (Low[0] < previous_low){
new_low2 = Low[0]; //it works only if next 1 candle is lower than previous. It fail if next 2 candle is lower than previous_low.
}
}
It can be achive by bubblesort
public class BubbleSort
{
void bubbleSort(double arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
System.out.print("first value :"+arr[j]+" greater than" +"previos :"+arr[j+1] + " ");
System.out.println();
// swap arr[j+1] and arr[i]
double temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
/* Prints the array */
void printArray(double arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method to test above
public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
double arr[] = {1.66, 1.55, 1.44};
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}

Resources