Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab04 #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions lab04-1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 7,
"id": "f1f97eff",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Neighbor relations:\n",
"{0: [49], 1: [2], 2: [3], 4: [4], 3: [4], 5: [7], 8: [5], 15: [4], 6: [2], 9: [6], 7: [3], 14: [6], 12: [5], 26: [4], 10: [3], 20: [9], 11: [4], 16: [7], 13: [3], 19: [3], 25: [5], 17: [3], 23: [3], 18: [24, 14], 24: [6], 35: [5], 21: [4], 34: [36], 22: [5], 28: [7], 32: [4], 30: [4], 29: [4], 27: [4], 33: [4], 38: [4], 37: [5], 31: [2], 36: [39], 41: [3], 42: [2], 44: [48, 45], 46: [2], 45: [4], 43: [5], 39: [2], 40: [41], 47: [2], 48: [4], 49: [3]}\n",
"\n",
"Neighbor histogram:\n",
"{1: [0, 1, 2, 4, 3, 5, 8, 15, 6, 9, 7, 14, 12, 26, 10, 20, 11, 16, 13, 19, 25, 17, 23, 24, 35, 21, 34, 22, 28, 32, 30, 29, 27, 33, 38, 37, 31, 36, 41, 42, 46, 45, 43, 39, 40, 47, 48, 49], 2: [18, 44]}\n"
]
}
],
"source": [
"def read_file(file):\n",
" gal_dict = {}\n",
" with open(file, 'r') as file:\n",
" for line in file:\n",
" part = line.strip().split()\n",
" point_id = int(part[0])\n",
" neighbors = []\n",
" for n in part[1:]:\n",
" try:\n",
" neighbors.append(int(n))\n",
" except ValueError:\n",
" print()\n",
" gal_dict[point_id] = neighbors\n",
" return gal_dict\n",
"\n",
"\n",
"def get_histogram(gal_dict):\n",
" \n",
" histogram = {}\n",
" for point_id, neighbors in gal_dict.items():\n",
" num_neighbors = len(neighbors)\n",
" if num_neighbors not in histogram:\n",
" histogram[num_neighbors] = []\n",
" histogram[num_neighbors].append(point_id)\n",
" return histogram\n",
"\n",
"def check(gal_dict):\n",
" \n",
" for point_id, neighbors in gal_dict.items():\n",
" for neighbor_id in neighbors:\n",
" if point_id not in gal_dict.get(neighbor_id, []):\n",
" return True\n",
" return False\n",
"\n",
"def test():\n",
" \n",
" gal_dict = read_file(\"Lab04-1.gal\")\n",
" print(\"Neighbor relations:\")\n",
" print(gal_dict)\n",
"\n",
" histogram = get_histogram(gal_dict)\n",
" print(\"\\nNeighbor histogram:\")\n",
" print(histogram)\n",
"\n",
" asymmetry = check(gal_dict)\n",
"\n",
"if __name__ == \"__main__\":\n",
" test()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fcd1a3ff",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
133 changes: 133 additions & 0 deletions lab04-2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"id": "35feb1af",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Summary Statistics:\n",
"Average Connectivity: 8.08\n",
"Max Connectivity: 24 (Node 4 )\n",
"Min Connectivity: 1 (Nodes 0 )\n",
"No disconnected nodes.\n"
]
}
],
"source": [
"class Node:\n",
" def __init__(self, node_id):\n",
" self.node_id = node_id\n",
" self.neighbors = []\n",
"\n",
" def add_neighbor(self, neighbor):\n",
" self.neighbors.append(neighbor)\n",
"\n",
" def __repr__(self):\n",
" return f\"Node {self.node_id}: {self.neighbors}\"\n",
"\n",
"class Graph:\n",
" def __init__(self):\n",
" self.nodes = {}\n",
"\n",
" def add_edge(self, node1, node2):\n",
" if node1 not in self.nodes:\n",
" self.nodes[node1] = Node(node1)\n",
" if node2 not in self.nodes:\n",
" self.nodes[node2] = Node(node2)\n",
" self.nodes[node1].add_neighbor(node2)\n",
" self.nodes[node2].add_neighbor(node1)\n",
"\n",
" def average_connectivity(self):\n",
" total_connectivity = sum(len(node.neighbors) for node in self.nodes.values())\n",
" return total_connectivity / len(self.nodes)\n",
"\n",
" def max_connectivity(self):\n",
" max_node = max(self.nodes.values(), key=lambda node: len(node.neighbors))\n",
" return len(max_node.neighbors), max_node\n",
"\n",
" def min_connectivity(self):\n",
" min_connectivity = min(len(node.neighbors) for node in self.nodes.values() if len(node.neighbors) > 0)\n",
" min_nodes = [node for node in self.nodes.values() if len(node.neighbors) == min_connectivity]\n",
" return min_connectivity, min_nodes\n",
"\n",
" def disconnected_nodes(self):\n",
" disconnected = [node for node in self.nodes.values() if len(node.neighbors) == 0]\n",
" return disconnected\n",
"\n",
"def build_graph_from_gal(file_path):\n",
" graph = Graph()\n",
" with open(file_path, 'r') as file:\n",
" for line in file:\n",
" parts = line.strip().split()\n",
" node_id = int(parts[0])\n",
" neighbors = []\n",
" for n in parts[1:]:\n",
" try:\n",
" neighbors.append(int(n))\n",
" except ValueError:\n",
" print()\n",
" for neighbor in neighbors:\n",
" graph.add_edge(node_id, neighbor)\n",
" return graph\n",
"\n",
"\n",
"\n",
"def summarize_graph(graph):\n",
" print(\"Summary Statistics:\")\n",
" print(\"Average Connectivity:\", graph.average_connectivity())\n",
" max_connectivity, max_node = graph.max_connectivity()\n",
" print(\"Max Connectivity:\", max_connectivity, \"(Node\", max_node.node_id, \")\")\n",
" min_connectivity, min_nodes = graph.min_connectivity()\n",
" min_node_ids = ', '.join(str(node.node_id) for node in min_nodes)\n",
" print(\"Min Connectivity:\", min_connectivity, \"(Nodes\", min_node_ids, \")\")\n",
" disconnected = graph.disconnected_nodes()\n",
" if disconnected:\n",
" disconnected_ids = ', '.join(str(node.node_id) for node in disconnected)\n",
" print(\"Disconnected Nodes:\", disconnected_ids)\n",
" else:\n",
" print(\"No disconnected nodes.\")\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" graph = build_graph_from_gal(\"Lab04-1.gal\")\n",
" summarize_graph(graph)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19ee2083",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}