0% found this document useful (0 votes)
22 views2 pages

AdjListArray C

This C code defines functions for generating and traversing an adjacency list representation of a graph. The graphGenrator function prompts the user to enter the number of vertices and generates an adjacency list representation by allocating memory for each list and calling the createList function. The createList function prompts the user to enter the vertices connected to a given vertex and returns the adjacency list. The graphTransversal function performs a breadth-first search on the graph by using an visited array to track discovered vertices.

Uploaded by

new
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
22 views2 pages

AdjListArray C

This C code defines functions for generating and traversing an adjacency list representation of a graph. The graphGenrator function prompts the user to enter the number of vertices and generates an adjacency list representation by allocating memory for each list and calling the createList function. The createList function prompts the user to enter the vertices connected to a given vertex and returns the adjacency list. The graphTransversal function performs a breadth-first search on the graph by using an visited array to track discovered vertices.

Uploaded by

new
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

7/25/23, 1:51 PM adjListArray.

adjListArray.c

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #define s scanf
6 #define p printf
7 int *createList();
8 int **graphGenrator()
9 {
10 int vertex;
11 p("how many vertex are in the graph?:\t");
12 s("%d", &vertex);
13 int vertex_list[vertex];
14 int *graph[vertex];
15 for (int i = 0; i < vertex; i++)
16 {
17 graph[i] = createList(&vertex_list);
18 }
19 return graph;
20 }
21 int *createList(int *vertex_list[])
22 {
23 int connected_vertex;
24 static int count;
25 p("how many things are connected with %d vertex:\t", ++count);
26 s("%d", &connected_vertex);
27
28 (*vertex_list)[count-1]=connected_vertex;//
29
30 int adj_list[connected_vertex];
31 for (int i = 0; i < connected_vertex; i++)
32 {
33 p("enter the %d connected vertex :", i + 1);
34 s("%d", &adj_list[i]);
35 }
36 return adj_list;
37 }
38
39 int is_visited(int arr[],int size,int val){
40 for(int i = 0; i < size; i++){
41 if(arr[i] == val) return 1;
42 else continue;
43 }
44 return 0;
45 }
46 void graphTransversal(int **graph, int vertex,int vertex_list[])
47 {
48 int visited[vertex],top=-1;
49 for (int i = 0; i < vertex; i++)
50 {
51 visited[i] = -1;
52 }

localhost:57785/99efb748-e89a-471f-8e07-7f3f3f172272/ 1/2
7/25/23, 1:51 PM adjListArray.c
53
54 while(top<vertex){
55 for(int i = 0; i < vertex; i++){
56 for(int j = 0; j <vertex_list[i];j++){
57 if (is_visited(visited, vertex, graph[i][j]))
58 {
59 continue;
60 }
61 else
62 {
63 visited[++top] = graph[i][j];
64 }
65 }
66 }
67 }
68 for(int i=0;i<vertex;i++){p("%d\t",visited[i]);}
69 }
70 int main()
71 {
72 int **graph1=graphGenrator();
73
74 return 0;
75 }
76  

localhost:57785/99efb748-e89a-471f-8e07-7f3f3f172272/ 2/2

You might also like