Professional Documents
Culture Documents
i/j : 1 2 3 4
1:0101
2:1010
3:0101
4:1010
Матрицата на соседството претставена со следниот граф е:
i/j: 1 2 3 4
1:0100
2:0001
3:1001
4:0100
Влезна датотека
4 // јазли
5 //рабови
1 2 //го покажува работ од јазол 1 до јазол 2
2 4 // го покажува работ од јазол 2 до јазол 4
3 1 // го покажува работ од јазол 3 до јазол 1
3 4 // го покажува работ од јазол 3 до јазол 4
4 2 // го покажува работ од јазол 4 до јазол 2
Code
#include <iostream>
bool A[10][10];
void initialize()
{
for(int i = 0;i < 10;++i)
for(int j = 0;j < 10;++j)
A[i][j] = false;
}
int main()
{
int x, y, nodes, edges;
initialize(); //Since there is no edge initially
cin >> nodes; //Number of nodes
cin >> edges; //Number of edges
for(int i = 0;i < edges;++i)
{
cin >> x >> y;
A[x][y] = true; //Mark the edges from vertex x to vertex y
}
if(A[3][4] == true)
cout << “There is an edge between 3 and 4” << endl;
else
cout << “There is no edge between 3 and 4” << endl;
if(A[2][3] == true)
cout << “There is an edge between 2 and 3” << endl;
else
cout << “There is no edge between 2 and 3” << endl;
return 0;
}
Output
The other way to represent a graph is by using an adjacency list. An adjacency list is an array A of separate lists. Each
element of the array Aiis a list, which contains all the vertices that are adjacent to vertex i.
For a weighted graph, the weight or cost of the edge is stored along with the vertex in the list using pairs. In an
undirected graph, if vertex j is in list Ai then vertex i will be in list Aj.
The space complexity of adjacency list is O(V + E) because in an adjacency list information is stored only for those
edges that actually exist in the graph. In a lot of cases, where a matrix is sparse using an adjacency matrix may not be
very useful. This is because using an adjacency matrix will take up a lot of space where most of the elements will be 0,
anyway. In such cases, using an adjacency list is better.
Note: A sparse matrix is a matrix in which most of the elements are zero, whereas a dense matrix is a matrix in which
most of the elements are non-zero.
Consider the same undirected graph from an adjacency matrix. The adjacency list of the graph is as follows:
A1 → 2 → 4
A2 → 3 → 1
A3 → 4 → 2
A4 → 3 → 1
Consider the same directed graph from an adjacency matrix. The adjacency list of the graph is as follows:
A1 → 2
A2 → 4
A3 → 1 → 4
A4 → 2
Consider the directed graph given above. The code for this graph is as follows:
Input file
4 // nodes
5 //edges
1 2 //showing edge from node 1 to node 2
2 4 //showing edge from node 2 to node 4
3 1 //showing edge from node 3 to node 1
3 4 //showing edge from node 3 to node 4
4 2 //showing edge from node 4 to node 2
Code
#include<iostream >
#include < vector >
int main()
{
int x, y, nodes, edges;
cin >> nodes; //Number of nodes
cin >> edges; //Number of edges
for(int i = 0;i < edges;++i)
{
cin >> x >> y;
adj[x].push_back(y); //Insert y in adjacency list of x
}
for(int i = 1;i <= nodes;++i)
{
cout << "Adjacency list of node " << i << ": ";
for(int j = 0;j < adj[i].size();++j)
{
if(j == adj[i].size() - 1)
cout << adj[i][j] << endl;
else
cout << adj[i][j] << " --> ";
}
}
return 0;
}
Output
Output
0
0
enter the values for adjacency matrix row:6
0
0
1
0
0
0
1
1
enter the values for adjacency matrix row:7
0
0
0
0
0
0
0
0
enter the values for adjacency matrix row:8
0
0
0
0
0
0
0
0
Оваа C++ programa, користејќи рекрузија, го прикажува бројот на вкрстени рабови во графот. Вкрстениот раб е
оној во кој јазолот укажува на друг јазол кој веќе е целосно посетен. Еве го изворниот код на програмата C++за
да ги прикаже вкрстените рабови, како и кога се среќаваат.
1. /*
2. * C++ Program to Find all Cross Edges in a Graph
3. */
4. #include<iostream>
5. #include<conio.h>
6. using namespace std;
7. struct node_info
8. {
9. int no;
10. int lv_time,st_time;
11. }*q = NULL, *r = NULL;
12. struct node
13. {
14. node_info *pt;
15. node *next;
16. }*top = NULL, *p = NULL, *np = NULL;
17. struct node1
18. {
19. node1 *link;
20. node_info *pt1;
21. }*head = NULL, *m = NULL, *n = NULL, *np1 = NULL;
22. int c = 0;
23. void push(node_info *ptr)
24. {
25. np = new node;
26. np->pt = ptr;
27. np->next = NULL;
28. if (top == NULL)
29. {
30. top = np;
31. }
32. else
33. {
34. np->next = top;
35. top = np;
36. }
37. }
38. node_info *pop()
39. {
40. if (top == NULL)
41. {
42. cout<<"underflow\n";
43. }
44. else
45. {
46. p = top;
47. top = top->next;
48. return(p->pt);
49. delete(p);
50. }
51. }
52. void store(node_info *ptr1)
53. {
54. np1 = new node1;
55. np1->pt1 = ptr1;
56. np1->link = NULL;
57. if (c == 0)
58. {
59. head = np1;
60. m = head;
61. m->link = NULL;
62. c++;
63. }
64. else
65. {
66. m = head;
67. np1->link = m;
68. head = np1;
69. }
70. }
71. int search(int j)
72. {
73. node1 *t = head;
74. while (t != NULL)
75. {
76. if ((t->pt1)->no == j)
77. {
78. break;
79. }
80. else
81. {
82. t = t->link;
83. continue;
84. }
85. }
86. return (t->pt1)->lv_time;
87. }
88. int present_in_stack(int j)
89. {
90. int flag = 0;
91. p = top;
92. while (p != NULL)
93. {
94. if ((p->pt)->no == j)
95. {
96. flag = 1;
97. return flag;
98. }
99. p = p->next;
100. }
101. return flag;
102. }
103. void topo(int *v,int am[][8],int i)
104. {
105. q = new node_info;
106. q->no = i;
107. q->st_time = c;
108. push(q);
109. v[i] = 1;
110. for (int j = 0; j < 8; j++)
111. {
112. if (am[i][j] == 0)
113. continue;
114. else if ((am[i][j] == 1 && v[j] == 1))
115. {
116. if (!present_in_stack(j) && q->st_time > search(j))
117. {
118. cout<<"Cross edge between "<<i<<" and "<<j<<endl;
119. }
120. continue;
121. }
122. else if(am[i][j] == 1 && v[j] == 0)
123. {
124. c++;
125. topo(v,am,j);
126. }
127. }
128. c++;
129. q = pop();
130. q->lv_time = c;
131. store(q);
132. return;
133. }
134. int main()
135. {
136. int v[8], am[8][8];
137. for (int i = 0; i < 8; i++)
138. v[i] = 0;
139. for (int i = 0; i < 8; i++)
140. {
141. cout<<"enter the values for adjacency matrix row:"<<i +
1<<endl;
142. for(int j = 0; j < 8; j++)
143. {
144. cin>>am[i][j];
145. }
146. }
147. topo(v,am,0);
148. getch();
149. }
Output
0
0
0
0
0
0
0
0
enter the values for adjacency matrix row:8
0
0
0
0
0
0
0
0
Cross edge between 5 and 2
Output
enter the values for adjacency matrix row:1
0
1
1
0
0
1
1
enter the values for adjacency matrix row:2
1
0
0
0
0
0
0
enter the values for adjacency matrix row:3
1
0
0
0
0
0
1
enter the values for adjacency matrix row:4
0
0
0
0
1
1
0
enter the values for adjacency matrix row:5
0
0
0
1
0
1
1
enter the values for adjacency matrix row:6
1
0
0
1
1
0
0
enter the values for adjacency matrix row:7
1
0
1
0
1
0
0