Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 73db1de

Browse files
Code
1 parent bbd0e71 commit 73db1de

10 files changed

+983
-0
lines changed

‎Graphs/Adjacent Matrices Method/.idea/.gitignore‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Graphs/Adjacent Matrices Method/.idea/Adjacent Matrices Method.iml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Graphs/Adjacent Matrices Method/.idea/inspectionProfiles/profiles_settings.xml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Graphs/Adjacent Matrices Method/.idea/misc.xml‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Graphs/Adjacent Matrices Method/.idea/modules.xml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Graphs/Adjacent Matrices Method/.idea/vcs.xml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 5
6+
}
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "ff457e68-2fa9-4848-bc5e-7ceedac64bf6",
6+
"metadata": {},
7+
"source": [
8+
"# Undirected Graphs\n",
9+
"\n",
10+
"In contrast to directed graphs, undirected graphs are a type of graph where edges have no inherent direction. The edges in an undirected graph represent symmetric relationships between vertices, meaning they connect vertices in both directions.\n",
11+
"\n",
12+
"In undirected graphs, the relationships are mutual; if there is an edge between vertex A and vertex B, it implies the existence of an edge from B to A. Undirected graphs are often used to model symmetric relationships like friendships in a social network, communication links in a computer network, or connections between locations in a map.\n",
13+
"\n",
14+
"Undirected graphs are simpler in structure compared to directed graphs, and they are widely employed in various fields, including computer science, social network analysis, and transportation planning.\n",
15+
"\n",
16+
"These two types of graphs, directed and undirected, serve as fundamental representations for modeling relationships and connections in a wide range of applications.\n"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 11,
22+
"id": "2525d8d3-cf7b-4b34-a38a-bc7ac2d5f683",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"class UndirectedGraph:\n",
27+
" def __init__(self, no_of_vertices):\n",
28+
" self.no_of_vertices = no_of_vertices\n",
29+
" self.adj_matrix = [[0] * no_of_vertices for _ in range(no_of_vertices)]\n",
30+
"\n",
31+
" def add_edge(self, vertex1, vertex2, weight=1):\n",
32+
" self.adj_matrix[vertex1][vertex2] = weight\n",
33+
" self.adj_matrix[vertex2][vertex1] = weight # For undirected graph\n",
34+
"\n",
35+
" def remove_edge(self, vertex1, vertex2):\n",
36+
" self.adj_matrix[vertex1][vertex2] = 0\n",
37+
" self.adj_matrix[vertex2][vertex1] = 0\n",
38+
"\n",
39+
" def display(self):\n",
40+
" for row in self.adj_matrix:\n",
41+
" print(row)\n",
42+
"\n",
43+
" def add_vertex(self):\n",
44+
" self.no_of_vertices += 1\n",
45+
" # Add a new row for the new vertex\n",
46+
" self.adj_matrix.append([0] * self.no_of_vertices)\n",
47+
" # Add a new column for the new vertex in existing rows\n",
48+
" for row in self.adj_matrix:\n",
49+
" row.append(0)\n",
50+
"\n",
51+
" def remove_vertex(self, vertex):\n",
52+
" if vertex < self.no_of_vertices:\n",
53+
" del self.adj_matrix[vertex]\n",
54+
" self.no_of_vertices -= 1\n",
55+
"\n",
56+
" for row in self.adj_matrix:\n",
57+
" del row[vertex]\n",
58+
"\n",
59+
" def get_neighbors(self, vertex):\n",
60+
" if vertex < self.no_of_vertices:\n",
61+
" return [index for index, weight in enumerate(self.adj_matrix[vertex]) if weight > 0]\n",
62+
" else:\n",
63+
" return []\n",
64+
"\n",
65+
" def has_edge(self, vertex1, vertex2):\n",
66+
" return self.adj_matrix[vertex1][vertex2] > 0 or self.adj_matrix[vertex2][vertex1] > 0\n",
67+
"\n",
68+
" def graph_size(self):\n",
69+
" return self.no_of_vertices, sum(row.count(1) for row in self.adj_matrix)\n",
70+
"\n",
71+
" def clear_graph(self):\n",
72+
" self.no_of_vertices = 0\n",
73+
" self.adj_matrix = []\n"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 12,
79+
"id": "93c1bc88-0d14-48b5-be60-e35cd9b25d7c",
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"graph = UndirectedGraph(5)"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 13,
89+
"id": "ddf3c951-2efa-4a53-b2c8-4753b01342f1",
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"name": "stdout",
94+
"output_type": "stream",
95+
"text": [
96+
"Undirected Graph:\n",
97+
"[0, 2, 1, 0, 0]\n",
98+
"[2, 0, 0, 3, 0]\n",
99+
"[1, 0, 0, 2, 0]\n",
100+
"[0, 3, 2, 0, 1]\n",
101+
"[0, 0, 0, 1, 0]\n"
102+
]
103+
}
104+
],
105+
"source": [
106+
"graph.add_edge(0, 1, 2)\n",
107+
"graph.add_edge(0, 2, 1)\n",
108+
"graph.add_edge(1, 3, 3)\n",
109+
"graph.add_edge(2, 3, 2)\n",
110+
"graph.add_edge(3, 4, 1)\n",
111+
"\n",
112+
"print(\"Undirected Graph:\")\n",
113+
"graph.display()"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 14,
119+
"id": "1b29be66-e589-47b0-860b-6385eed7c6f0",
120+
"metadata": {},
121+
"outputs": [
122+
{
123+
"name": "stdout",
124+
"output_type": "stream",
125+
"text": [
126+
"\n",
127+
"Undirected Graph after removing edge (1, 3):\n",
128+
"[0, 2, 1, 0, 0]\n",
129+
"[2, 0, 0, 0, 0]\n",
130+
"[1, 0, 0, 2, 0]\n",
131+
"[0, 0, 2, 0, 1]\n",
132+
"[0, 0, 0, 1, 0]\n"
133+
]
134+
}
135+
],
136+
"source": [
137+
"graph.remove_edge(1, 3)\n",
138+
"\n",
139+
"print(\"\\nUndirected Graph after removing edge (1, 3):\")\n",
140+
"graph.display()"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 15,
146+
"id": "c39ec39b-d0fe-431e-9ba1-54d80adc5ed5",
147+
"metadata": {},
148+
"outputs": [
149+
{
150+
"name": "stdout",
151+
"output_type": "stream",
152+
"text": [
153+
"\n",
154+
"Undirected Graph after adding a vertex:\n",
155+
"[0, 2, 1, 0, 0, 0]\n",
156+
"[2, 0, 0, 0, 0, 0]\n",
157+
"[1, 0, 0, 2, 0, 0]\n",
158+
"[0, 0, 2, 0, 1, 0]\n",
159+
"[0, 0, 0, 1, 0, 0]\n",
160+
"[0, 0, 0, 0, 0, 0, 0]\n"
161+
]
162+
}
163+
],
164+
"source": [
165+
"graph.add_vertex()\n",
166+
"\n",
167+
"print(\"\\nUndirected Graph after adding a vertex:\")\n",
168+
"graph.display()"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": 16,
174+
"id": "d8e1a783-d7c0-442b-ba41-2b3736e66184",
175+
"metadata": {},
176+
"outputs": [
177+
{
178+
"name": "stdout",
179+
"output_type": "stream",
180+
"text": [
181+
"\n",
182+
"Undirected Graph after removing vertex 1:\n",
183+
"[0, 1, 0, 0, 0]\n",
184+
"[1, 0, 2, 0, 0]\n",
185+
"[0, 2, 0, 1, 0]\n",
186+
"[0, 0, 1, 0, 0]\n",
187+
"[0, 0, 0, 0, 0, 0]\n"
188+
]
189+
}
190+
],
191+
"source": [
192+
"graph.remove_vertex(1)\n",
193+
"\n",
194+
"print(\"\\nUndirected Graph after removing vertex 1:\")\n",
195+
"graph.display()"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 17,
201+
"id": "cde71770-2e32-4444-96ea-20452decdd90",
202+
"metadata": {},
203+
"outputs": [
204+
{
205+
"name": "stdout",
206+
"output_type": "stream",
207+
"text": [
208+
"\n",
209+
"Neighbors of vertex 0: [1]\n"
210+
]
211+
}
212+
],
213+
"source": [
214+
"neighbors = graph.get_neighbors(0)\n",
215+
"print(\"\\nNeighbors of vertex 0:\", neighbors)"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"execution_count": 18,
221+
"id": "6a4b61b7-1881-432d-b63a-9c0a8ca33a56",
222+
"metadata": {},
223+
"outputs": [
224+
{
225+
"name": "stdout",
226+
"output_type": "stream",
227+
"text": [
228+
"\n",
229+
"Does edge (0, 2) exist? False\n"
230+
]
231+
}
232+
],
233+
"source": [
234+
"has_edge = graph.has_edge(0, 2)\n",
235+
"print(\"\\nDoes edge (0, 2) exist?\", has_edge)"
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": 19,
241+
"id": "75cd8bc2-2800-4c49-b7b1-ff28e5bbd82c",
242+
"metadata": {},
243+
"outputs": [
244+
{
245+
"name": "stdout",
246+
"output_type": "stream",
247+
"text": [
248+
"\n",
249+
"Graph Size (Vertices, Edges): (5, 4)\n"
250+
]
251+
}
252+
],
253+
"source": [
254+
"size = graph.graph_size()\n",
255+
"print(\"\\nGraph Size (Vertices, Edges):\", size)"
256+
]
257+
},
258+
{
259+
"cell_type": "code",
260+
"execution_count": 20,
261+
"id": "bed90a7d-1cbb-469c-8ea0-f5da0b5fee85",
262+
"metadata": {},
263+
"outputs": [
264+
{
265+
"name": "stdout",
266+
"output_type": "stream",
267+
"text": [
268+
"\n",
269+
"Undirected Graph after clearing:\n"
270+
]
271+
}
272+
],
273+
"source": [
274+
"graph.clear_graph()\n",
275+
"\n",
276+
"print(\"\\nUndirected Graph after clearing:\")\n",
277+
"graph.display()"
278+
]
279+
},
280+
{
281+
"cell_type": "code",
282+
"execution_count": null,
283+
"id": "de72da94-99ed-4ab2-9e79-cfb1de482577",
284+
"metadata": {},
285+
"outputs": [],
286+
"source": []
287+
}
288+
],
289+
"metadata": {
290+
"kernelspec": {
291+
"display_name": "Python 3 (ipykernel)",
292+
"language": "python",
293+
"name": "python3"
294+
},
295+
"language_info": {
296+
"codemirror_mode": {
297+
"name": "ipython",
298+
"version": 3
299+
},
300+
"file_extension": ".py",
301+
"mimetype": "text/x-python",
302+
"name": "python",
303+
"nbconvert_exporter": "python",
304+
"pygments_lexer": "ipython3",
305+
"version": "3.10.6"
306+
}
307+
},
308+
"nbformat": 4,
309+
"nbformat_minor": 5
310+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /