Lattices of Partial Order Set Segmentation Fault(core dumped) - c

Given a Set S = {x1,x2,x3,x4,x5,….} whose size is n (< 20), I need to list all the lattices (subsets of S) on S with size k(should be taken as input, but in my question I fixed it for debugging purposes). The relation over which the partial order is defined is divisibility.
#include <stdio.h>
#include <stdbool.h>
typedef struct{
int data;
struct node* next;
}node;
void addEdge(node* graph[],int a,int b){
node *ptr=(node *)malloc(sizeof(node));
ptr->next=graph[a];
ptr->data=b;
graph[a]=ptr;
}
void buildGraph(node* graph[],int n){
int i,j;
for(i=2;i<=n;i++){
for(j=1;j<=i/2;j++){
if(i%j==0){
if(j!=1) addEdge(graph,i/j,i);
else addEdge(graph,j,i);
}
}
}
}
void printGraph(node* graph[],int n){
int i=0;
node *ptr;
for(i=0;i<=n;i++){
ptr=graph[i];
printf("Row-%d ",i);
while(ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
}
void print(int array[],int n){
int i;
for(i=0;i<n;i++) printf("%d ",array[i]);
printf("\n");
}
bool checkDuplicate(int array[],int n,int a){
int i;
for(i=0;i<n;i++) if(array[i]==a) return false;
return true;
}
void formLattice(node* graph[],int lattice[],int k,int vertex,int level){
//printf("Level-%d \n",level);
if(level>k) return;
if(level==k) {
lattice[level-1]=vertex;
print(lattice,k);
return;
}
lattice[level-1]=vertex;
node* ptr=graph[vertex];
while(ptr!=NULL){
// if(checkDuplicate(lattice,level,ptr->data))
formLattice(graph,lattice,k,ptr->data,level+1);
ptr=ptr->next;
}
}
void printLattice(node* graph[],int n,int k){
int lattice[k],i;
node* ptr;
for(i=1;i<=n;i++){
ptr=graph[i];
while(ptr!=NULL){
lattice[0]=i;
formLattice(graph,lattice,k,ptr->data,2);
ptr=ptr->next;
}
}
}
int main(void) {
int n=10,k=4;
node* graph[n+1];
int i;
for(i=0;i<=n;i++) graph[i]=NULL;
buildGraph(graph,n);
// printGraph(graph,n); // Comment to print out the Hasse Diagram
printLattice(graph,n,k);
return 0;
}
Showing Segementation Fault(core dumped)
Any idea why is this happening so?
Thanks in Advance

The code of formLattice() has evidences that only those vertexes are probed for lattice[level] that are connected to lattice[level-1]. Not every lattice, of course, has a total order on it (namely {1,2,3,6} doesnt). How can the present program choose the vertex 3 for lattice[2] while the vertex 2 is stored into lattice[1]?
In other words, there must be another loop, going for all already chosen vertexes in lattice from 0 to level-1, around the present loop for ptr.

Related

implementation of queue using two stacks

#include<stdio.h>
#include<stdlib.h>
struct stack
{
int top,size,top1;
int *arr;
};
int isempty(struct stack *ptr)
{
if(ptr->top==-1){return 1;}
else{return 0;}
}
int isfull(struct stack *ptr)
{
if(ptr->top==ptr->size-1){return 1;}
else{return 0;}
}
int push(struct stack *ptr, int data)
{
if(isfull(ptr)){printf("Stack is Full\n");}
else{
ptr->top++;
ptr->arr[ptr->top]=data;
}
}
int pop(struct stack *ptr)
{
if(isempty(ptr)){printf("Stack is empty\n");}
else{
int a=ptr->arr[ptr->top];
ptr->top--;
return a;
}
}
int pop2(struct stack *ptr)
{
struct stack *lol;
printf("!");
lol->top1=-1;
printf("!");
lol->size=100;
printf("!");
lol->arr=(int*)malloc(lol->size*sizeof(int));
}
int main()
{
struct stack *ptr;
ptr->top=-1;
ptr->size=100;
ptr->arr=(int*)malloc(ptr->size*sizeof(int));
push(ptr,1);
push(ptr,2);
push(ptr,3);
push(ptr,4);
push(ptr,5);
pop2(ptr);
printf("%d ",pop(ptr));
printf("%d ",pop(ptr));
printf("%d ",pop(ptr));
printf("%d ",pop(ptr));
printf("%d ",pop(ptr));
}
i am trying to run this code but dont konw why but in function pop2 the second and third printf are not working or in other terms pop2 is not working. it is not throwing any error but just stops after sometime. if i remove pop2 than the whole code is working properly

Maintaining a queue using structures in C

If I input 'E 5' I need to enqueue 5 onto the queue with no output. If I input 'D' I need to print the dequeued element.
Below is my code:
#include<stdio.h>
#include <stdlib.h>
typedef struct queue{
int start;//first element index
int end;//last element index
int size;//length of queue
int *ptr;//actual queue
}queue;
queue* createQueue(int x){
queue* Q=(queue*)malloc(sizeof(queue));
Q->start=0;
Q->end=0;
Q->size=1;
Q->ptr[0]=x;
return Q;
}
void enqueue(int x, queue*qptr, int i){
qptr->size++;//increase size
qptr->end++;//increase index of last element
qptr->ptr[i]=x;
return;
}
void dequeue(queue*qptr){
if(qptr->size==0){
printf("Empty\n");
return;
}
printf("%d\n",qptr->ptr[qptr->start]);
qptr->start++;
qptr->size--;
}
int main(){
char a; int i=0;int j;queue* qptr;
while(scanf("%c",&a)!=-1){
if(a=='E'){
if(i==0){
scanf("%d",&j);
qptr =createQueue(j);
i++;
}
else{
scanf("%d",&j);
enqueue(j, qptr, i);
i++;
}
}
else if(a=='D'){
if(i==0){printf("Empty\n");}
else {dequeue(qptr);i++;}
}
}
return 0;
}
I don't understand what are my errors because it seems correct to me but the code doesn't compile. Please help me.

Finding path between nodes using BFS in C language

I'm new to C language and it's been harder for me to work with pointers after working in Java😥
I was trying to write a code of finding a path (not necessary minimum) between two nodes in a graph using breadth-first-search.
Here is my code :
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 200
void push(int a);
int pop(void);
void bfs(int a,int b,int len);
int nextnode(int a);
typedef struct node{
int data;
struct node* next;
}node;
int res[MAXSIZE];
int visited[MAXSIZE];
int rear,front;
node* graph[MAXSIZE];
int len;
int path[MAXSIZE];
int nextnode(int a)
{
if(graph[a]==NULL)
return -1;
else
{
struct node* c=graph[a];
while(visited[c->data]!=1 && c!=NULL)
{
c=c->next;
}
if(c==NULL)
return -1;
else
return c->data;
}
}
void push(int a)
{
path[rear]=a;
rear++;
}
int pop()
{
if(front==rear)
return -1;
int num=path[front];
front++;
return num;
}
int main()
{
rear=0;
len=0;
front=0;
int n,e;
int i,a,b;
printf("%s\n%s", "Inputting Graph... ","Enter number of nodes and edges: ");
scanf("%d %d",&n,&e);
printf("%s %d %s\n", "Graph Created with",n,"nodes without any edge.");
printf("%s\n","Enter the edges in 1 2 format if an edge exist from Node 1 to Node 2" );
for(i=1;i<=n;i++)
{
graph[i]=NULL;
visited[i]=0;
}
struct node* new = (struct node*)malloc(sizeof(struct node));
for(i=0;i<e;i++)
{
scanf("%d %d",&a,&b);
new->data=b;
new->next=NULL;
struct node* curr=graph[a];
if(curr==NULL)
{
graph[a]=new;
}
else
{
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=new;
}
}
printf("%s\n", "Graph Created Successfully.");
printf("%s", "Enter the node numbers between which the path is to be found between: ");
scanf("%d %d",&a,&b);
bfs(a,b,0);
printf("Length is %d\n",len);
for(i=1;i<=len;i++)
{
printf("%d\n",res[len]);
}
}
void bfs(int a,int b,int len)
{
int c;
visited[a]=1;
int flag=0;
while(a!=-1)
{
c=nextnode(a);
while(c!=-1)
{
c=nextnode(a);
if(c==b)
{
flag=1;
break;
}
push(c);
visited[c]=1;
}
len++;
res[len]=a;
if(flag==1)
{
res[len]=b;
break;
}
a=pop();
}
}
I know it's huge, but please mind going through it once. The problem I'm getting is Segmentation Fault after I input all the values, and before dfs() function call! Please Help.
For understanding: I have used array of Lists. Each array index denotes a node and the list denotes all the edges it is connected to. eg: if my Graph has 1->2, 1->3, 2-3 edges;
graph[1] will have a list 2->3->NULL. And graph[2] will have 3->NULL.
Thank you.
EDIT
As pointed out by Aditi, the error was in the line where nextnode function ran the while loop. After changing the code to
while(c != NULL && visited[c->data] == 1 )
the program ran flawlessly.
Thanks!
I think what you are trying to do is not graph[i] = NULL but graph[i]->next = NULL

Storing input from user into linked list using array

So I'm trying to get input from user and store it in linked list, using array (every 5 chars a new linked list is created). After getting EOF I want to print the input (actually print the arrays in each linked list)
here is my code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
typedef struct charNode {
int arr[MAX];
struct charNode *next;
} charNode;
void addNode();
void printAll();
int main(){
int c,i;
charNode *head=malloc(sizeof(charNode));
charNode *current=head;
while((c=getchar())!=EOF){
while(i<MAX){
current->arr[i++]=c;
}
i=0;
addNode(current);
}
printAll(head);
return 0;
}
void addNode(charNode *current){
struct charNode *link = (struct charNode*) malloc(sizeof(struct charNode));
current->next =link;
link->next = NULL;
current=current->next;
}
void printAll(charNode *head){
int j=0;
while(head->next!=NULL){
while(j<MAX){
printf("\n %d \t",head->arr[j++]);
}
printAll(head->next);
}
return;
}
and I'm getting "Segmentation fault (core dumped)" error..
This is Undefined behavior:
int c,i;
charNode *head=malloc(sizeof(charNode));
charNode *current=head;
while((c=getchar())!=EOF){
while(i<MAX){
current->arr[i++]=c;
}
since you are using i uninitialized to access your array, which could produce the Segmentation fault.
Change this:
printf("\n %d \t",head->arr[j++]);
to this:
printf("\n %c \t",head->arr[j++]);
so that you print characters, instead of numbers.
Simply initialize i to 0 for a start, happy debugging! =)
int main(){
char c;
charNode *head=malloc(sizeof(charNode));
charNode *current=head;
int i = 0;
while((c=getchar())!=EOF){
getchar();
while(i<MAX){
current->arr[i++]=c;
}
i=0;
addNode(&current);
}
printAll(head);
return 0;
}
void addNode(charNode **current){
struct charNode *link = malloc(sizeof(struct charNode));
link->next = NULL;
(*current)->next =link;
*current=(*current)->next;
}
void printAll(charNode *head){
int j=0;
if(head!=NULL){
while(j<MAX){
printf("%c\n",head->arr[j++]);
}
if(head->next != NULL)
printAll(head->next);
}
}
I changed your code a bit. Especially take a good look at addNode(). Now it works correctly

Random graph with structures

I am trying to implement code for a random graph, where all vertices are connected to each other. The edges should be randomly selected. I wrote this code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define VOL 100
struct node{
int info;
struct node *next;
};
struct node *read_list(void){
struct node *p, *first=NULL;
int i,V;
for(i=0;i<V;i++){
p=malloc(sizeof(struct node));
p->next=first;
p->info=rand()%V;
first=p;
}
return(first);
}
void print_list(struct node *p){
while(p!=NULL){
printf("%d-> ", p->info);
p=p->next;
}
printf("NULL\n");
return;
}
int read_graph(struct node *G[]){
int i, V;
printf("Select a number of vertices:\n");
scanf("%d", &V);
for(i=0;i<V;i++){
printf("Adjacency list of vertex %d:\n", i);
G[i]=read_list();
}
return(V);
}
void print_graph(struct node *G[], int V){
int i;
printf("Adjacency lists of the graph:\n");
for(i=0;i<V;i++){
printf("Adjacency vertices to %d: ",i);
print_list(G[i]);
}
return;
}
int adj(int i, int j, struct node *G[]){
int r;
struct node *p;
p=G[i];
while (p!=NULL && p->info !=j)
p=p->next;
if(p==NULL)
r=1;
else
r=0;
return (r);
}
int main(){
srand(time(NULL));
struct node *G[VOL], *L;
int V;
V=read_graph(G);
print_graph(G, V);
L=read_list();
return 0;
}
However, it doesn't work and I don't know why. Xcode tells me 'build succeeded" but the code prints nothing (currently only 'Select a number of vertices'): no adjacency list, no edges.. Could you please check it and tell me where the errors are?
In the for statement below, the variable V is not initialized.
...
struct node *read_list(void) {
struct node *p, *first = NULL;
int i, V; // <<<<<<<<<<<<<<<<<<< V not initialized
for (i = 0; i<V; i++) {
//^ trouble here
...

Resources