1. ## Graph data structure

I'm trying to code a function that checks if there is an edge between two nodes in directed graph. I found the code for this function in C++ on the internet and rewrote it into C, but i get error, can someone please help me!

the code in C++
link to the code : Find the path between given vertices in a directed graph – Techie Delight   Code:
```The code that I wrote in C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 40

{
int dest;
};

{

struct Graph
{
int V;
int * visited;
};

{
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}

struct Graph* createGraph(int V)
{
struct Graph* graph =
(struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;

// Create an array of adjacency lists.  Size of
// array will be V
graph->array =

// Initialize each adjacency list as empty by
int i;
for (i = 0; i < V; ++i)

return graph;
}

int countVertices(struct Graph * graph){

return graph->V;
}

int countEdges(struct Graph* graph){
int v = countVertices(graph);
int sum = 0;

for(int i = 0; i < v; i++){
while(node != NULL){
node = node->next;
sum++;
}
}

return sum/2;
}

void addEdge(struct Graph* graph, int src, int dest)
{
// Add an edge from src to dest.  A new node is
// is added at the beginning

}

void printGraph(struct Graph* graph)
{
int v;
for (v = 0; v < graph->V; ++v)
{
while (pCrawl)
{
printf("-> %d", pCrawl->dest);
pCrawl = pCrawl->next;
}
printf("\n");
}
}

struct queue {
int items[SIZE];
int front;
int rear;
};

// Create a queue
struct queue* createQueue() {
struct queue* q = malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;
return q;
}

// Check if the queue is empty
int isEmpty(struct queue* q) {
if (q->rear == -1)
return 1;
else
return 0;
}

void enqueue(struct queue* q, int value) {
if (q->rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}

// Removing elements from queue
int dequeue(struct queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
printf("Resetting queue ");
q->front = q->rear = -1;
}
}
return item;
}

bool isConnected(struct Graph* graph, int startVertex, int destVertex) {
struct queue* q = createQueue();

graph->visited[startVertex] = 1;
enqueue(q, startVertex);

while (!isEmpty(q)) {
int currentVertex = dequeue(q);
printf("Visited %d\n", currentVertex);
if(currentVertex == destVertex)
return 1;

while(temp){
}
temp = temp->next;
}
}
return 1;
}

int main()
{
struct Graph* graph = createGraph(4);

int src = 1, dest = 2;
if(isConnected(graph, src, dest)){
printf("path exists");
}
else
printf("No path");
return 0;
}``` 2. You aren't allocating any memory for the visited array. 3. I allocated memory for visited array and now it prints "path exists" all the time, no matter the input.
this is how i did it:
Code:
```bool bfs(struct Graph* graph, int startVertex, int destVertex) {
struct queue* q = createQueue();

int v = countVertices(graph);
graph->visited = (int*)malloc(v * sizeof(int));

graph->visited[startVertex] = 1;
enqueue(q, startVertex);

while (!isEmpty(q)) {
//printQueue(q);
int currentVertex = q->items[q->front];
currentVertex = dequeue(q);
//printf("Visited %d\n", currentVertex);
if(currentVertex == destVertex)
return 1;

while(temp){
}
temp = temp->next;
}
}
return 1;
}```
What did i do wrong this time? 4. Originally Posted by Shahiddd I allocated memory for visited array and now it prints "path exists" all the time, no matter the input.
this is how i did it:
Code:
```bool bfs(struct Graph* graph, int startVertex, int destVertex) {
struct queue* q = createQueue();

int v = countVertices(graph);
graph->visited = (int*)malloc(v * sizeof(int));

graph->visited[startVertex] = 1;
enqueue(q, startVertex);

while (!isEmpty(q)) {
//printQueue(q);
int currentVertex = q->items[q->front];
currentVertex = dequeue(q);
//printf("Visited %d\n", currentVertex);
if(currentVertex == destVertex)
return 1;

while(temp){
}
temp = temp->next;
}
}
return 1;
}```
What did i do wrong this time?
What is the lifetime of the data?
Because you appear to delete the data at the top of the function and you never give it a good initial value.

Tim S. 5. All of your return paths return 1.
Presumably the last one should return 0.  Popular pages Recent additions data structure, graph, int, vertex 