{"id":37107645,"url":"https://github.com/deltam/perm","last_synced_at":"2026-01-14T12:56:01.641Z","repository":{"id":46028569,"uuid":"205840142","full_name":"deltam/perm","owner":"deltam","description":"Permutation generator based on group theory written in Go","archived":false,"fork":false,"pushed_at":"2021-11-20T06:19:41.000Z","size":1588,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-19T23:14:05.824Z","etag":null,"topics":["golang","permutation-generator"],"latest_commit_sha":null,"homepage":"","language":"Go","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/deltam.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}},"created_at":"2019-09-02T11:17:05.000Z","updated_at":"2024-06-19T23:14:05.825Z","dependencies_parsed_at":"2022-07-26T07:47:18.680Z","dependency_job_id":null,"html_url":"https://github.com/deltam/perm","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/deltam/perm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deltam%2Fperm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deltam%2Fperm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deltam%2Fperm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deltam%2Fperm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deltam","download_url":"https://codeload.github.com/deltam/perm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deltam%2Fperm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28420814,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T10:47:48.104Z","status":"ssl_error","status_checked_at":"2026-01-14T10:46:19.031Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["golang","permutation-generator"],"created_at":"2026-01-14T12:56:01.100Z","updated_at":"2026-01-14T12:56:01.634Z","avatar_url":"https://github.com/deltam.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"https://raw.githubusercontent.com/deltam/perm/master/img/perm5_ribbon.png\" width=\"100%\" alt=\"n=5 permutation order graph as TAOCP 7.2.1.2 Fig.22 style\"\u003e\n\u003c/div\u003e\n\n# perm\n\n[![GoDev]( https://pkg.go.dev/badge/github.com/deltam/perm)](https://pkg.go.dev/github.com/deltam/perm)\n\nPermutation generator based on group theory[^1].\n\n- Fast\n- Stateless\n    - generate next permutation from current permutation only.\n- Permutation order is **NOT** lexical.\n\n## Installation\n\n```\ngo get github.com/deltam/perm\n```\n\n## Usage\n\nIndex only:\n\n```go\nfunc main() {\n\tg, err := perm.New(3)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tfor ; !g.Done(); g.Next() {\n\t\tfmt.Println(p)\n\t}\n}\n// Output:\n// [1 2 0]\n// [2 0 1]\n// [0 2 1]\n// [2 1 0]\n// [0 1 2]\n// [1 0 2]\n```\n\nPermutation of slice:\n\n```go\nfunc main() {\n\twords := []rune(\"ABC\")\n\tg, err := perm.Iter(ss)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tfor ; !g.Done(); g.Next() {\n\t\tfmt.Printf(\"%v\\t%s\\n\", g.Index(), string(words))\n\t}\n}\n// Output:\n// [1 2 0]\tABC\n// [2 0 1]\tBCA\n// [0 2 1]\tCBA\n// [2 1 0]\tBAC\n// [0 1 2]\tCAB\n// [1 0 2]\tACB\n```\n\n## Performance\n\nThis algorithm is 40~50% faster than naive recursive algorithm.\n\n\u003ctable class='benchstat '\u003e\n\u003ctbody\u003e\n\u003ctr\u003e\u003cth\u003e\u003cth\u003etime/op\n\u003ctr\u003e\u003ctd\u003ePrimitive-8\u003ctd\u003e33.3µs ± 2%\n\u003ctr\u003e\u003ctd\u003eGenerator-8\u003ctd\u003e39.1µs ± 2%\n\u003ctr\u003e\u003ctd\u003ePermRecursive-8\u003ctd\u003e66.9µs ± 1%\n\u003c/tbody\u003e\n\u003c/table\u003e\n\n\n```go\nfunc recPerm(p []int, n int, ignore []bool) {\n\tif n == 0 {\n\t\treturn\n\t}\n\tfor i := 0; i \u003c len(p); i++ {\n\t\tif !ignore[i] {\n\t\t\tp[n-1] = i\n\t\t\tignore[i] = true\n\t\t\trecPerm(p, n-1, ignore)\n\t\t\tignore[i] = false\n\t\t}\n\t}\n}\n```\n\n## Background\n\nSee [paper](https://arxiv.org/abs/1307.2549)[^1] for details.\n\nI also explained details on [my blog post](https://deltam.blogspot.com/2019/12/permutationgenerator.html)(Japanese only). [^3]\n\n[Cayley graph](https://en.wikipedia.org/wiki/Cayley_graph) on permutation group generated by _sigma_ and _tau_ has a size two disjoint cycle cover and [Hamilton path](https://en.wikipedia.org/wiki/Hamiltonian_path).\n\n    sigma(rotation): (1, 2, ..., n) -\u003e (2, 3, ..., n, 1)\n    tau(swap):       (1, 2, ..., n) -\u003e (2, 1, ..., n)\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"https://raw.githubusercontent.com/deltam/perm/master/img/cycle_cover4.png\" width=\"80%\" alt=\"cycle cover by n=4\"\u003e\n\u003c/div\u003e\n\nBelow is the Hamilton path created by split and join the two cycles.\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"https://raw.githubusercontent.com/deltam/perm/master/img/hamilton_path4.png\" width=\"80%\" alt=\"hamilton path by n=4\"\u003e\n\u003c/div\u003e\n\nBy observing above Hamilton path, you can discover local rule for generating that.\n\nSee `primitive.go` or this text[^2][^3] for details.\n\n## License\n\nMIT\n\nCopyright 2019 deltam\n\n\n[^1]: [Hamiltonicity of the Cayley Digraph on the Symmetric Group Generated by σ = (1 2 ... n) and τ = (1 2)](https://arxiv.org/abs/1307.2549)\n\n[^2]: [The Williams Construction: Superpermutations — Greg Egan](https://www.gregegan.net/SCIENCE/Superpermutations/Superpermutations.html#WILLIAMS)\n\n[^3]: [2つの操作のみで全順列を列挙する：対称群のグラフ上のハミルトン路にもとづく順列生成の紹介と実装 : サルノオボエガキ](https://deltam.blogspot.com/2019/12/permutationgenerator.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeltam%2Fperm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeltam%2Fperm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeltam%2Fperm/lists"}