{"id":18782588,"url":"https://github.com/h4kor/graph-force","last_synced_at":"2025-04-30T13:53:22.243Z","repository":{"id":63959411,"uuid":"571719016","full_name":"H4kor/graph-force","owner":"H4kor","description":"Python library for embedding large graphs in 2D space, using force-directed layouts.","archived":false,"fork":false,"pushed_at":"2022-11-28T18:59:48.000Z","size":34,"stargazers_count":163,"open_issues_count":4,"forks_count":0,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-19T18:53:50.231Z","etag":null,"topics":["force-directed-graphs","graph-algorithms","python","python3"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/graph-force/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/H4kor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-11-28T18:36:33.000Z","updated_at":"2025-02-12T06:39:49.000Z","dependencies_parsed_at":"2022-11-29T16:31:44.615Z","dependency_job_id":null,"html_url":"https://github.com/H4kor/graph-force","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/H4kor%2Fgraph-force","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/H4kor%2Fgraph-force/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/H4kor%2Fgraph-force/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/H4kor%2Fgraph-force/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/H4kor","download_url":"https://codeload.github.com/H4kor/graph-force/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251715102,"owners_count":21631835,"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":["force-directed-graphs","graph-algorithms","python","python3"],"created_at":"2024-11-07T20:36:34.233Z","updated_at":"2025-04-30T13:53:22.115Z","avatar_url":"https://github.com/H4kor.png","language":"Rust","readme":"# Graph Force\n\nA python/rust library for embedding graphs in 2D space, using force-directed layouts.\n\n## Installation\n\n```bash\npip install graph_force\n```\n\n## Usage\n\nThe first parameter defines the number of nodes in graph.\nThe second parameter is an iterable of edges, where each edge is a tuple of two integers representing the nodes it connects. Node ids start at 0. \n\n\n```python\nimport graph_force\n\nedges = [(0, 1), (1, 2), (2, 3), (3, 0)]\npos = graph_force.layout_from_edge_list(4, edges)\n```\n\n### Example with networkx\n\nThis library does not have a function to consume a networkx graph directly, but it is easy to convert it to an edge list.\n\n```python\nimport networkx as nx\nimport graph_force\n\nG = nx.grid_2d_graph(10, 10)\n# we have to map the names to integers\n# as graph_force only supports integers as node ids at the moment\nedges = []\nmapping = {n: i for i, n in enumerate(G.nodes)}\ni = 0\nfor edge in G.edges:\n    edges.append((mapping[edge[0]], mapping[edge[1]]))\n\npos = graph_force.layout_from_edge_list(len(G.nodes), edges, iter=1000)\nnx.draw(G, {n: pos[i] for n, i in mapping.items()}, node_size=2, width=0.1)\n```\n\n### Example with edge file\n\nThis methods can be used with large graphs, where the edge list does not fit into memory.\n\nFormat of the file:\n- Little endian\n- 4 bytes: number of nodes(int)\n- 12 bytes: nodeA(int), nodeB(int), weight(float)\n\n```python\nimport graph_force\nimport struct\n\nwith open(\"edges.bin\", \"rb\") as f:\n    n = 10\n    f.write(struct.pack(\"i\", n))\n    for x in range(n-1):\n        f.write(struct.pack(\"iif\", x, x+1, 1))\n\npos = graph_force.layout_from_edge_file(\"edges.bin\", iter=50)\n```\n\n\n### Options\n\n`iter`, `threads` and `model`, `initial_pos` are optional parameters, supported by `layout_from_edge_list` and `layout_from_edge_file`.\n\n```python\npos = graph_force.layout_from_edge_list(\n    number_of_nodes,\n    edges,\n    iter=500,  # number of iterations, default 500\n    threads=0,  # number of threads, default 0 (all available)\n    model=\"spring_model\", # model to use, default \"spring_model\", other option is \"networkx_model\"\n    initial_pos=[(0.4, 0.7), (0.7, 0.2), ...], # initial positions, default None (random)\n)\n```\n#### Available models\n\n- `spring_model`: A simple spring model (my own implementation)\n- `networkx_model`: Reimplementation of the [spring model from networkx](https://networkx.org/documentation/stable/reference/generated/networkx.drawing.layout.spring_layout.html)\n\n## Contributing\n\n- [Development](DEVELOPMENT.md)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh4kor%2Fgraph-force","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fh4kor%2Fgraph-force","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh4kor%2Fgraph-force/lists"}