You are on page 1of 17

Матрица на соседство е матрица составена само од единици и нули – ако

постои патека меѓу две темиња, тогаш се запишува 1, а во спротивно 0. Кај


орграфот, постоењето патека е ограничено со насоката на реброто.

Матрица на соседството на следниот граф е претставена:

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>

using namespace std;

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

There is an edge between 3 and 4.

There is no edge between 2 and 3.


Adjacency list

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 >

using namespace std;

vector <int> adj[10];

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

 Adjacency list of node 1: 2

 Adjacency list of node 2: 4

 Adjacency list of node 3: 1 --> 4

 Adjacency list of node 4: 2


C++ Program to Implement all Forward Edges in a Graph
Оваа C++ програма, користејќи рекурзија, го прикажува бројот на раборви на
графот. Предниот раб е оној во кој јазлот укаќува на еден од неговоите потомци.
Еве го изворниот ко на програмата C++ за прикажување на задните рабови како
и кога се среќаваат.
Излезот на програмата е прикажан подолу.
1. /*
2. * C++ Program to Implement all Forward 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 && !present_in_stack(j))
115. {
116. if (q->st_time < search(j))
117. {
118. cout<<"\nForward Edge between "<<i<<" and "<<j<<endl;
119. }
120. continue;
121. }
122. else if (am[i][j] == 1 && v[j] == 0)
123. {
124. c++;
125. cout<<"\nForward Edge between "<<i<<" and "<<j<<endl;
126. topo(v,am,j);
127. }
128. }
129. c++;
130. q = pop();
131. q->lv_time = c;
132. store(q);
133. return;
134. }
135. int main()
136. {
137. int v[8],am[8][8];
138. for (int i = 0; i < 8; i++)
139. v[i] = 0;
140. for (int i = 0; i < 8; i++)
141. {
142. cout<<"enter the values for adjacency matrix row:"<<i +
1<<endl;
143. for(int j = 0; j < 8; j++)
144. {
145. cin>>am[i][j];
146. }
147. }
148. topo(v,am,0);
149. getch();
150. }

Output

enter the values for adjacency matrix row:1


0
1
0
0
1
0
0
1
enter the values for adjacency matrix row:2
0
0
1
0
0
0
0
0
enter the values for adjacency matrix row:3
0
0
0
1
0
0
0
0
enter the values for adjacency matrix row:4
0
1
0
0
0
0
0
0
enter the values for adjacency matrix row:5
0
0
0
0
0
1

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

Forward Edge between 0 and 1

Forward Edge between 1 and 2

Forward Edge between 2 and 3

Forward Edge between 0 and 4

Forward Edge between 4 and 5

Forward Edge between 5 and 6

Forward Edge between 5 and 7

Forward Edge between 0 and 7

C++ Program to Implement Find all Cross Edges in a Graph

Оваа 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

enter the values for adjacency matrix row:1


0
1
0
0
1
0
0
1
enter the values for adjacency matrix row:2
0
0
1
0
0
0
0
0
enter the values for adjacency matrix row:3
0
0
0
1
0
0
00
0
enter the values for adjacency matrix row:4
0
1
0
0
0
0
0
0
enter the values for adjacency matrix row:5
0
0
0
0
0
1
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
Cross edge between 5 and 2

C++ Program to Find all Back Edges in a Graph

Изворниот код на back edges за тоа како и кога се среќаваат.


1. /*
2. * C++ Program to Find all Back 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. }*q = NULL,*r = NULL;
11. struct node
12. {
13. node_info *pt;
14. node *next;
15. }*top = NULL,*p = NULL,*np = NULL;
16. void push(node_info *ptr)
17. {
18. np = new node;
19. np->pt = ptr;
20. np->next = NULL;
21. if (top == NULL)
22. {
23. top = np;
24. }
25. else
26. {
27. np->next = top;
28. top = np;
29. }
30. }
31. node_info *pop()
32. {
33. if (top == NULL)
34. {
35. cout<<"underflow\n";
36. }
37. else
38. {
39. p = top;
40. top = top->next;
41. return(p->pt);
42. delete(p);
43. }
44. }
45. void back_edges(int *v,int am[][7],int i,int k)
46. {
47. cout<<"\n\nDISPLAYING BACK EDGES\n\n";
48. q = new node_info;
49. q->no = i;
50. push(q);
51. v[i] = 1;
52. for (int j = 0; j < 7; j++)
53. {
54. if (am[i][j] == 1 && v[j] == 0)
55. {
56. c++;
57. back_edges(v,am,j,i);
58. }
59. else if (am[i][j] == 0)
60. continue;
61. else if ((j == k) && (am[i][k] == 1 && v[j] == 1))
62. continue;
63. else
64. {
65. cout<<"Back edge present between "<<i<<" th node and "<<j<<" th
node"<<endl;
66. am[i][j] = 0;
67. am[j][i] = 0;
68. continue;
69. }
70. }
71. r = pop();
72. return;
73. }
74. int main()
75. {
76. int v[7],am[7][7];
77. for (int i = 0; i < 7; i++)
78. v[i] = 0;
79. for (int i = 0; i < 7; i++)
80. {
81. cout<<"enter the values for adjacency matrix row:"<<i + 1<<endl;
82. for(int j = 0; j < 7; j++)
83. {
84. cin>>am[i][j];
85. }
86. }
87. back_edges(v,am,0,0);
88. getch();
89. }

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

DISPLAYING BACK EDGES

Back edge present between 6 th node and 0 th node


Back edge present between 5 th node and 0 th node
Back edge present between 5 th node and 4 th node

You might also like