{"id":26722803,"url":"https://github.com/asadiahmad/max-independent-set","last_synced_at":"2025-07-31T23:34:58.256Z","repository":{"id":259971305,"uuid":"879949924","full_name":"AsadiAhmad/Max-Independent-Set","owner":"AsadiAhmad","description":"Calculating Max Independent Set with greedy algorithm","archived":false,"fork":false,"pushed_at":"2024-11-20T21:21:50.000Z","size":830,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-20T21:42:31.293Z","etag":null,"topics":["algorithm","algorithms","anytree","greedy-algorithms","independent-set","max-independent-set","networkx","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AsadiAhmad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-28T20:54:24.000Z","updated_at":"2024-11-20T21:21:54.000Z","dependencies_parsed_at":"2024-10-28T21:52:09.314Z","dependency_job_id":null,"html_url":"https://github.com/AsadiAhmad/Max-Independent-Set","commit_stats":null,"previous_names":["asadiahmad/max-independent-set"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsadiAhmad%2FMax-Independent-Set","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsadiAhmad%2FMax-Independent-Set/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsadiAhmad%2FMax-Independent-Set/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsadiAhmad%2FMax-Independent-Set/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AsadiAhmad","download_url":"https://codeload.github.com/AsadiAhmad/Max-Independent-Set/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245922846,"owners_count":20694605,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","algorithms","anytree","greedy-algorithms","independent-set","max-independent-set","networkx","python"],"created_at":"2025-03-27T20:37:20.195Z","updated_at":"2025-03-27T20:37:20.751Z","avatar_url":"https://github.com/AsadiAhmad.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Max-Independent-Set\nCalculating Max Independent Set with greedy algorithm\n\n## Tech :hammer_and_wrench: Languages and Tools :\n\u003cdiv\u003e\n  \u003cimg src=\"https://github.com/devicons/devicon/blob/master/icons/python/python-original.svg\" title=\"Python\" alt=\"Python\" width=\"40\" height=\"40\"/\u003e\u0026nbsp;\n  \u003cimg src=\"https://github.com/devicons/devicon/blob/master/icons/networkx/networkx-original.svg\"  title=\"Networkx\" alt=\"Networkx\" width=\"40\" height=\"40\"/\u003e\u0026nbsp;\n  \u003cimg src=\"https://github.com/devicons/devicon/blob/master/icons/matplotlib/matplotlib-original.svg\"  title=\"MatPlotLib\" alt=\"MatPlotLib\" width=\"40\" height=\"40\"/\u003e\u0026nbsp;\n\u003c/div\u003e\n\nFor Tree : anytree Lib and from scratch tree for O(n) algortihm\n\nFor Graph : networkX Lib and matplotlib for showing the graph\n\n## Max Independent Set for Tree\n\n### Time complexity : O(n*h(n))\n\nAlways return correct answer (Always return Max Independent Set)\n\n\u003cimg src=\"/Pictures/1.png\"/\u003e\n\n### Prove : \n\n**Step 1 : Initialization** The initial set ```unCutNodesSet``` is constructed by adding the root and all descendants. Using ```Root.descendants``` from the ```anytree``` library, this operation takes O(n), where n is the number of nodes in the tree.\n\n**Step 2 : Independent Set Iteration** \n- Checking if a node has no children (leaf check) is O(1).\n- Appending to ```IndependentSet``` and removing from ```unCutNodesSet``` are both O(1).\n- For each non-leaf node, the function calls itself recursively on each child. and we can say if we have k nodes under the selected subtree then complexity should be O(k)\n- what does this section do ? it found all of leaf nodes of a subtree then add those to the independent set list then remove all leafs and preant leafs from the tree.\n\n**Step 3 : Main Loop** The main loop iterates as long as unCutNodesSet is not empty. Within each iteration:\n- Choosing a node in ```unCutNodesSet```: The line ```next(iter(unCutNodesSet))``` takes O(1) time.\n- Calling ```IndependentSetIteration```: The core of the complexity lies in this recursive function.\n\n**Total Complexity :** The while loop calls Independent Set Iteration function O(h(n)) times since Complexity of the Independent Set Iteration function is O(n) then the total complexity should be O(n) + O(n* h(n)) = O(n*h(n))\n\n**Best Case :** Now if we have a full tree it shows the Best case and the complexity should be Ω(n*Log(n)).\n\n**Worst Case :** if we had a line tree then is shows the worst case and the complexity should be O(n^2).\n\n## Optimized Max Independent Set for Tree\n\n### Time complexity : O(n)\n\nAlways return correct and optimal answer (Always return Max Independent Set)\n\n\u003cimg src=\"/Pictures/6.png\"/\u003e\n\n### Prove : \n\n**Step 1 : Tree and Nodes Initialization** \n- Constructing each ```Node``` runs in O(1) wtih attributes like ```data```, ```parent```, ```children```.\n- Since constructing the whole tree wtih n ```Nodes``` costs O(n).\n\n**Step 2 : Iteration costs** every steps in while itration costs O(1) and we can say that each iteration costs O(1). \n\n**Step 2.1 : Constructing Leafs** \n- In the initialization step we add leafs of the tree dynamically each one cost O(1) and for whole tree costs O(n).\n\n**Step 2.2 : Accessing to a Leaf Node** \n- Accessing to a random leaf in a set ```next(iter(tree.leafs))``` Costs O(1)\n\n**Step 2.3 : Adding the Leaf to the Independent Set**\n- Appending the leaf to the ```IndependentSet``` list is O(1).\n\n**Step 2.4 : Removing the Leaf**\n- Removing the leaf from the ```tree.leafs``` set is O(1) because removing elements from a set is an O(1) operation.\n\n**Step 2.5 : Removing the Parent and Grandparent**\n- Removing grandparent children ```leaf.parent.parent.children.remove(leaf.parent)``` costs O(1) because removing elements from a set is an O(1) operation.\n- Removing the grandparent ```leaf.parent.parent = None``` costs O(1)\n- Removing Parent ```leaf.parent = None``` costs O(1)\n\n**Step 2.6 : Update Leafs set**\n- Adding a node to leafs set ```tree.leafs.add(leaf.parent.parent)``` costs O(1)\n\n**Step 3 : Function costs** Each iteration of the while loop costs O(1) since we do this to achive all leafs and then remove the leafs now a graph has n nodes at worst case and we can say that function costs O(n)\n \n## Max Independent Set for Graph\n\n### Time complexity : O(V^2+VE)\n\nSometimes return correct answer (because its greedy algorithm)\n\n### Correct answer :\n\n\u003cimg src=\"/Pictures/3.png\"/\u003e\n\n\u003cimg src=\"/Pictures/2.png\"/\u003e\n\n\u003cimg src=\"/Pictures/4.png\"/\u003e\n\n### Wrong answer :\n\n\u003cimg src=\"/Pictures/5.png\"/\u003e\n\nthe answer should be : F and E for the A, B, C, E, F, G set and a D node for whole graph then the answer should be : E, F, D but in greedy way we first choose G then can't choose E and F then we just should choose a Node from the complete-4 graph A, B, C, D and the greedy answer being G and A which is incorrect.\n\n## Conclusion :\n\nGreedy algorithm works correct for trees not graphs\n\nAlso greedy algorithm for tree has better complexity\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasadiahmad%2Fmax-independent-set","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasadiahmad%2Fmax-independent-set","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasadiahmad%2Fmax-independent-set/lists"}