From b30b951c9db399d8b88aaaa9e9c7582563c54b0f Mon Sep 17 00:00:00 2001 From: BoB <bo.bernhardsson@control.lth.se> Date: Mon, 23 Dec 2024 15:49:42 +0000 Subject: [PATCH] day23 det sa bara click --- day 23/day_23_bob.ipynb | 202 +++++++++++++++++++++++++++ {day 023 => day 23}/day_23_felix.cpp | 0 {day 023 => day 23}/day_23_max_n.cpp | 0 3 files changed, 202 insertions(+) create mode 100644 day 23/day_23_bob.ipynb rename {day 023 => day 23}/day_23_felix.cpp (100%) rename {day 023 => day 23}/day_23_max_n.cpp (100%) diff --git a/day 23/day_23_bob.ipynb b/day 23/day_23_bob.ipynb new file mode 100644 index 0000000..d241c8d --- /dev/null +++ b/day 23/day_23_bob.ipynb @@ -0,0 +1,202 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "source": [ + "import time\n", + "from collections import defaultdict\n", + "\n", + "def readdata23(filename):\n", + " nodelist = []\n", + " with open(filename, 'r') as file:\n", + " for line in file:\n", + " nodelist.append(line.strip().split('-'))\n", + " return nodelist\n", + "edges = readdata23('input23.txt')\n", + "len(edges)" + ], + "metadata": { + "id": "vhRxJ1CeJ5jo", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "4b9eb8f3-4917-4894-afcc-d81218b0e49c" + }, + "execution_count": 1, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "3380" + ] + }, + "metadata": {}, + "execution_count": 1 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#part 1\n", + "def cliques(edges):\n", + " # Step 1: Create an adjacency list\n", + " adjacency_list = {}\n", + " for u, v in edges:\n", + " adjacency_list.setdefault(u, set()).add(v)\n", + " adjacency_list.setdefault(v, set()).add(u)\n", + " # Step 2: Find all cliques of size 3\n", + " cliques = set()\n", + " for u in adjacency_list:\n", + " for v in adjacency_list[u]:\n", + " if v > u:\n", + " for w in adjacency_list[v]:\n", + " if w > v and w in adjacency_list[u]:\n", + " cliques.add(tuple(sorted((u, v, w))))\n", + " return cliques\n", + "\n", + "lan3list = list(cliques(edges))\n", + "s = 0\n", + "for lan3 in lan3list:\n", + " if any(c.startswith('t') for c in lan3):\n", + " s += 1\n", + "print(s)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "TLa2i-Ub1hst", + "outputId": "e8043723-c0a1-4bf4-d4fd-53951fa6a4a1" + }, + "execution_count": 41, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "1599\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "def bron_kerbosch(R, P, X, graph, cliques):\n", + " global nrcalls\n", + " nrcalls += 1\n", + " if not P and not X: # Base case: no more nodes to explore\n", + " cliques.append(R)\n", + " return\n", + "\n", + " #for v in list(P): # next two lines give 'pivoting'\n", + " pivot = next(iter(P.union(X))) if P or X else None\n", + " for v in P - graph[pivot]:\n", + " bron_kerbosch(\n", + " R | {v}, # Add v to current clique\n", + " P & graph[v], # Nodes connected to v in P\n", + " X & graph[v], # Nodes connected to v in X\n", + " graph, cliques\n", + " )\n", + " P.remove(v) # Remove v from P\n", + " X.add(v) # Add v to X\n", + "\n", + "def find_largest_clique(edges):\n", + " # Step 1: Build adjacency list\n", + " graph = {}\n", + " for u, v in edges:\n", + " graph.setdefault(u, set()).add(v)\n", + " graph.setdefault(v, set()).add(u)\n", + "\n", + " # Step 2: Find all maximal cliques\n", + " cliques = []\n", + " bron_kerbosch(set(), set(graph.keys()), set(), graph, cliques)\n", + "\n", + " # Step 3: Find the largest clique\n", + " largest_clique = max(cliques, key=len)\n", + " return largest_clique\n", + "\n", + "# Run and time it\n", + "start_time = time.time()\n", + "nrcalls = 0\n", + "largest_clique = sorted(list(find_largest_clique(edges)))\n", + "elapsed_time = time.time() - start_time\n", + "print(\"Number of calls:\", nrcalls)\n", + "print(f\"--- {elapsed_time:.3f} seconds ---\")\n", + "print(\"Largest clique:\", ','.join(largest_clique))" + ], + "metadata": { + "id": "6KXRu2TT4Wi_" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "**Without pivoting:** \\\\\n", + "Number of calls: 248068 \\\\\n", + "--- 0.370 seconds --- \\\\\n", + "\n", + "\n", + "**With pivoting:** \\\\\n", + "Number of calls: 2595 \\\\\n", + "--- 0.014 seconds --- \\\\" + ], + "metadata": { + "id": "J8XKaZ2eIMHv" + } + }, + { + "cell_type": "code", + "source": [ + "graph = {}\n", + "for u, v in edges:\n", + " graph.setdefault(u, set()).add(v)\n", + " graph.setdefault(v, set()).add(u)\n", + "print(f\"number of nodes : {len(graph)}\")\n", + "\n", + "d = defaultdict(set)\n", + "for n in graph.keys():\n", + " d.setdefault(len(graph[n]), set()).add(n)\n", + "\n", + "print(f\"number of nodes with degree 13 : {len(d[13])}\")\n", + "# all nodes had degree 13" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "C8ygnKOmAtTY", + "outputId": "d4729b06-93e6-498d-a26b-73285d88289a" + }, + "execution_count": 47, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "number of nodes : 520\n", + "number of nodes with degree 13 : 520\n" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/day 023/day_23_felix.cpp b/day 23/day_23_felix.cpp similarity index 100% rename from day 023/day_23_felix.cpp rename to day 23/day_23_felix.cpp diff --git a/day 023/day_23_max_n.cpp b/day 23/day_23_max_n.cpp similarity index 100% rename from day 023/day_23_max_n.cpp rename to day 23/day_23_max_n.cpp -- GitLab