Skip to content

Commit 2bd7897

Browse files
finished bfs and fixed dfs
1 parent 7bc6b15 commit 2bd7897

File tree

7 files changed

+191
-9
lines changed

7 files changed

+191
-9
lines changed

0x06-graphs/4-depth_first_traverse

-54.2 KB
Binary file not shown.

0x06-graphs/4-depth_first_traverse.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ vertex_t *get_vertex_index(const graph_t *graph, size_t index)
2929
}
3030

3131
/**
32-
* depth_first_traverse - A function that goes through a graph using the
32+
* dfs_util - A function that goes through a graph using the
3333
* depth -first algorithm.
3434
* @v: index of the vertex
3535
* @visited: array of nodes marked visited or not
@@ -47,7 +47,7 @@ void dfs_util(int v, size_t *visited, size_t curr_depth, size_t *depth,
4747
edge_t *edge;
4848

4949
curr = get_vertex_index(graph, v);
50-
if (curr != NULL && visited[v] == BACKTRACK)
50+
if (curr != NULL && visited[v] != EXPLORED)
5151
{
5252
action(curr, curr_depth);
5353
if (curr_depth > *depth)
@@ -57,7 +57,7 @@ void dfs_util(int v, size_t *visited, size_t curr_depth, size_t *depth,
5757
while (edge != NULL)
5858
{
5959
dest = edge->dest;
60-
if (visited[dest->index] == BACKTRACK)
60+
if (visited[dest->index] != EXPLORED)
6161
{
6262
dfs_util(dest->index, visited, curr_depth + 1,
6363
depth, graph, action);
@@ -86,9 +86,9 @@ size_t depth_first_traverse(const graph_t *graph,
8686
visited = calloc(graph->nb_vertices, sizeof(size_t));
8787

8888
curr = graph->vertices;
89-
while (curr)
89+
if (curr)
9090
{
91-
if (visited[curr->index] == BACKTRACK)
91+
if (visited[curr->index] == UNEXPLORED)
9292
dfs_util(curr->index, visited, 0, &depth,
9393
graph, action);
9494
curr = curr->next;
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#include "graphs.h"
2+
3+
/**
4+
* createqueue_t - A function that creates a queue
5+
* Return: queue created
6+
*/
7+
queue_t *createqueue_t()
8+
{
9+
queue_t *q = malloc(sizeof(queue_t));
10+
11+
q->front = -1;
12+
q->rear = -1;
13+
return (q);
14+
}
15+
16+
/**
17+
* enqueue - A function that appends the item in queue
18+
* @q: queue
19+
* @value: value to be stored
20+
*/
21+
void enqueue(queue_t *q, int value)
22+
{
23+
if (q->rear == SIZE - 1)
24+
printf("\nqueue_t is Full!!");
25+
else
26+
{
27+
if (q->front == -1)
28+
q->front = 0;
29+
q->rear++;
30+
q->items[q->rear] = value;
31+
}
32+
}
33+
34+
/**
35+
* dequeue - A function that fetches first item in queue
36+
* @q: queue
37+
*
38+
* Return: first item in queue on success, -1 on failure
39+
*/
40+
int dequeue(queue_t *q)
41+
{
42+
int item;
43+
44+
if (q->rear == -1)
45+
{
46+
printf("queue_t is empty");
47+
item = -1;
48+
} else
49+
{
50+
item = q->items[q->front];
51+
q->front++;
52+
if (q->front > q->rear)
53+
{
54+
q->front = q->rear = -1;
55+
}
56+
}
57+
return (item);
58+
}
59+
60+
/**
61+
* get_vertex_index - A function that fetches an vertex existing in graph by
62+
* index
63+
* @graph: A pointer to the graph
64+
* @index: The index identifying the vertex
65+
*
66+
* Return: vertex on success, NULL on failure
67+
*/
68+
vertex_t *get_vertex_index(const graph_t *graph, size_t index)
69+
{
70+
vertex_t *node;
71+
72+
if (index > graph->nb_vertices)
73+
return (NULL);
74+
node = graph->vertices;
75+
if (node == NULL)
76+
return (NULL);
77+
while (node != NULL)
78+
{
79+
if (node->index == index)
80+
{
81+
return (node);
82+
}
83+
node = node->next;
84+
}
85+
return (NULL);
86+
}
87+
88+
/**
89+
* breadth_first_traverse - A function that goes through a graph using the
90+
* breadth -first algorithm.
91+
* @graph: A pointer to the graph to traverse
92+
* @action: A pointer to a function to be called for each visited vertex.
93+
* Return: The max breadth , or 0 on failure
94+
*/
95+
size_t breadth_first_traverse(const graph_t *graph,
96+
void (*action)(const vertex_t *v, size_t breadth))
97+
{
98+
size_t breadth = 0, *visited; int index = 0;
99+
vertex_t *curr, *dest; edge_t *edge; queue_t *q;
100+
101+
if (graph == NULL || graph->nb_vertices == 0 || graph->vertices == NULL)
102+
return (0);
103+
visited = calloc(graph->nb_vertices, sizeof(size_t));
104+
q = createqueue_t();
105+
curr = graph->vertices;
106+
enqueue(q, curr->index); enqueue(q, LEVELBREAK);
107+
while (q->rear != -1)
108+
{
109+
index = dequeue(q);
110+
if (index == LEVELBREAK)
111+
{
112+
breadth++;
113+
enqueue(q, LEVELBREAK);
114+
index = dequeue(q);
115+
if (index == LEVELBREAK)
116+
break;
117+
}
118+
curr = get_vertex_index(graph, index);
119+
if (visited[curr->index] != EXPLORED)
120+
{
121+
visited[curr->index] = EXPLORED;
122+
action(curr, breadth);
123+
edge = curr->edges;
124+
while (edge)
125+
{
126+
dest = edge->dest;
127+
if (dest && visited[dest->index] != EXPLORED)
128+
{
129+
enqueue(q, dest->index);
130+
visited[dest->index] = BACKTRACK;
131+
}
132+
edge = edge->next;
133+
}
134+
}
135+
}
136+
free(visited);
137+
return (breadth - 1);
138+
}

0x06-graphs/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77

88
* 1-graph_add_vertex.c - Contains a function that adds a vertex to an existing graph
99

10+
* 2-graph_add_edge.c - Contains a function that adds an edge to an existing graph
11+
12+
* 3-graph_delete.c - Contains a function that deletes a graph
13+
14+
* 4-depth_first_traverse.c - Contains a function that traverses the graph in dfs way and returns the depth of the graph
15+
16+
* 5-breadth_first_traverse.c - Contains a function that traverses the graph in bfs way and returns the depth of the graph
17+
18+
1019

1120
## Resources:
1221
* [[EYNTK] Graphs](https://intranet.hbtn.io/concepts/86)
1322
* [Graphs Wiki](https://en.wikipedia.org/wiki/Graph_%28abstract_data_type%29)
1423
* [Graphs (Stanford lecture)](http://web.stanford.edu/class/cs161/Lectures/Lecture9/CS161Lecture09.pdf)
15-
* [https://intranet.hbtn.io/rltoken/ZBxeNNgwtzIDZI1PLLdt6Q](https://www.youtube.com/watch?v=gXgEDyodOJU)
24+
* [Introduction to graphs](https://www.youtube.com/watch?v=gXgEDyodOJU)

0x06-graphs/graphs.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
#define SUCCESS 1
88
#define FAILURE 0
9+
#define UNEXPLORED 0
910
#define EXPLORED 1
10-
#define BACKTRACK 0
11+
#define BACKTRACK 2
12+
#define SIZE 1024
13+
#define LEVELBREAK -60
1114
/**
1215
* enum edge_type_e - Enumerates the different types of
1316
* connection between two vertices
@@ -70,6 +73,20 @@ typedef struct graph_s
7073
vertex_t *vertices;
7174
} graph_t;
7275

76+
/**
77+
* struct queue_s - Representation of a queue
78+
*
79+
* @items: items to be queued
80+
* @front: Pointer to the first item in queue
81+
* @rear: Pointer to the last item in queue
82+
*/
83+
typedef struct queue_s
84+
{
85+
int items[SIZE];
86+
int front;
87+
int rear;
88+
} queue_t;
89+
7390
void graph_display(const graph_t *graph);
7491
graph_t *graph_create(void);
7592
vertex_t *graph_add_vertex(graph_t *graph, const char *str);

0x06-graphs/tests/4-main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ int main(void)
3939
!graph_add_vertex(graph, "Chicago") ||
4040
!graph_add_vertex(graph, "Houston") ||
4141
!graph_add_vertex(graph, "Las Vegas") ||
42-
!graph_add_vertex(graph, "Boston"))
42+
!graph_add_vertex(graph, "Boston") ||
43+
!graph_add_vertex(graph, "Los Angeles"))
4344
{
4445
fprintf(stderr, "Failed to add vertex\n");
4546
return (EXIT_FAILURE);

0x06-graphs/tests/graphs.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
#define SUCCESS 1
88
#define FAILURE 0
9+
#define UNEXPLORED 0
910
#define EXPLORED 1
10-
#define BACKTRACK 0
11+
#define BACKTRACK 2
12+
#define SIZE 1024
13+
#define LEVELBREAK -60
1114
/**
1215
* enum edge_type_e - Enumerates the different types of
1316
* connection between two vertices
@@ -70,6 +73,20 @@ typedef struct graph_s
7073
vertex_t *vertices;
7174
} graph_t;
7275

76+
/**
77+
* struct queue_s - Representation of a queue
78+
*
79+
* @items: items to be queued
80+
* @front: Pointer to the first item in queue
81+
* @rear: Pointer to the last item in queue
82+
*/
83+
typedef struct queue_s
84+
{
85+
int items[SIZE];
86+
int front;
87+
int rear;
88+
} queue_t;
89+
7390
void graph_display(const graph_t *graph);
7491
graph_t *graph_create(void);
7592
vertex_t *graph_add_vertex(graph_t *graph, const char *str);

0 commit comments

Comments
 (0)