Skip to content

Commit

Permalink
cycle detenction
Browse files Browse the repository at this point in the history
  • Loading branch information
mzusin committed Nov 16, 2023
1 parent b45a87c commit 54bf1b5
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface IGraph<T> {
bfs: (callback: (label: Label) => void, startLabel?: Label) => void;
dfs: (callback: (label: Label) => void, startLabel?: Label) => void;
dfsRecursive: (callback: (label: Label, startLabel?: Label) => void) => void;

hasCycle: () => boolean;
}

export interface IAdjacencyListOptions<T> {
Expand Down Expand Up @@ -77,6 +79,8 @@ myGraph.dfs((label: Label) => {
myGraph.dfsRecursive((label: Label) => {
console.log(label);
});

console.log(myGraph.hasCycle()); // true or false
```

**Adjacency Matrix Representation**
Expand Down
2 changes: 2 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ declare module 'mz-graph' {
bfs: (callback: (label: Label, startLabel?: Label) => void) => void;
dfs: (callback: (label: Label, startLabel?: Label) => void) => void;
dfsRecursive: (callback: (label: Label) => void, startLabel?: Label) => void;

hasCycle: () => boolean;
}

export type AdjacencyMatrix<T> = T[][];
Expand Down
2 changes: 1 addition & 1 deletion dist/mz-graph.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/mz-graph.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/mz-graph.node.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ https://github.com/mzusin/mz-graph
MIT License
Copyright (c) 2023-present, Miriam Zusin
*/
var w=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var j=(l,o)=>{for(var u in o)w(l,u,{get:o[u],enumerable:!0})},A=(l,o,u,c)=>{if(o&&typeof o=="object"||typeof o=="function")for(let d of x(o))!I.call(l,d)&&d!==u&&w(l,d,{get:()=>o[d],enumerable:!(c=y(o,d))||c.enumerable});return l};var M=l=>A(w({},"__esModule",{value:!0}),l);var N={};j(N,{graph:()=>C,matrix:()=>k});module.exports=M(N);var C=l=>{let o=new Map,u=n=>{o.has(n.label)||o.set(n.label,[])},c=n=>{var t;return(t=o.get(n))!=null?t:null},d=(n,t)=>{var r,s;(r=o.get(n.label))==null||r.push(t),l.isDirected||(s=o.get(t.label))==null||s.push(n)},L=()=>{for(let[n,t]of o.entries()){let r=t.map(s=>{let b=s.value!==void 0?`(${s.value})`:"";return`${s.label}${b}`}).join(", ");console.log(`${n} -> [${r}]`)}},p=(n,t)=>{let r=new Set,s=e=>{let i=[e];for(r.add(e);i.length>0;){let a=i.shift();n(a);let h=c(a)||[];for(let f of h)r.has(f.label)||(r.add(f.label),i.push(f.label))}};if(t!==void 0){s(t);return}let b=o.keys();for(let e of b)r.has(e)||s(e)},v=(n,t)=>{let r=new Set,s=e=>{let i=[e];for(r.add(e);i.length>0;){let a=i.pop();n(a);let h=c(a)||[];for(let f of h)r.has(f.label)||(r.add(f.label),i.push(f.label))}};if(t!==void 0){s(t);return}let b=o.keys();for(let e of b)r.has(e)||s(e)},T=(n,t)=>{let r=new Set,s=e=>{if(r.has(e))return;r.add(e),n(e);let i=c(e);if(i)for(let a of i)s(a.label)};if(t!==void 0){s(t);return}let b=o.keys();for(let e of b)s(e)};return(()=>{if(!l.initial)return;let n=Object.keys(l.initial);for(let t of n){let r=l.initial[t]||[];o.set(t,r)}})(),{addVertex:u,getVertex:c,addEdge:d,printGraph:L,bfs:p,dfs:v,dfsRecursive:T}};var S=l=>{var o,u;if(l.initial){let c=l.initial.length,d=c>0?l.initial[0].length:0;return[c,d]}return[(o=l.rowsCount)!=null?o:0,(u=l.columnsCount)!=null?u:0]},k=l=>{let[o,u]=S(l),c=Array(o),d=()=>c,L=(n,t,r)=>{c[n][t]=r,l.isDirected||(c[t][n]=r)},p=()=>{for(let n=0;n<o;n++)console.log(c[n].map(t=>t==null?"-":t).join(" "))},v=n=>{let t=[[0,-1],[0,1],[-1,0],[1,0]],r=[[0,0]],s=[];for(let e=0;e<o;e++)s[e]=[];let b=(e,i)=>e>=0&&e<o&&i>=0&&i<u&&!s[e][i];for(;r.length>0;){let[e,i]=r.shift();if(!s[e][i]){s[e][i]=!0,n(e,i,c[e][i]);for(let a=0;a<t.length;a++){let[h,f]=t[a],m=e+h,g=i+f;b(m,g)&&r.push([m,g])}}}},T=n=>{let t=[[0,-1],[0,1],[-1,0],[1,0]],r=[[0,0]],s=[];for(let e=0;e<o;e++)s[e]=[];let b=(e,i)=>e>=0&&e<o&&i>=0&&i<u&&!s[e][i];for(;r.length>0;){let[e,i]=r.pop();if(!s[e][i]){s[e][i]=!0,n(e,i,c[e][i]);for(let a=0;a<t.length;a++){let[h,f]=t[a],m=e+h,g=i+f;b(m,g)&&r.push([m,g])}}}};return(()=>{if(l.initial){c=l.initial;return}for(let n=0;n<o;n++)c[n]=Array(u),l.defaultValue!==void 0&&c[n].fill(l.defaultValue)})(),{getMatrix:d,addEdge:L,printGraph:p,bfs:v,dfs:T}};0&&(module.exports={graph,matrix});
var T=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var C=(l,o)=>{for(var f in o)T(l,f,{get:o[f],enumerable:!0})},A=(l,o,f,c)=>{if(o&&typeof o=="object"||typeof o=="function")for(let g of x(o))!j.call(l,g)&&g!==f&&T(l,g,{get:()=>o[g],enumerable:!(c=I(o,g))||c.enumerable});return l};var S=l=>A(T({},"__esModule",{value:!0}),l);var N={};C(N,{graph:()=>k,matrix:()=>G});module.exports=S(N);var k=l=>{let o=new Map,f=t=>{o.has(t.label)||o.set(t.label,[])},c=t=>{var r;return(r=o.get(t))!=null?r:null},g=(t,r)=>{var e,n;(e=o.get(t.label))==null||e.push(r),l.isDirected||(n=o.get(r.label))==null||n.push(t)},p=()=>{for(let[t,r]of o.entries()){let e=r.map(n=>{let a=n.value!==void 0?`(${n.value})`:"";return`${n.label}${a}`}).join(", ");console.log(`${t} -> [${e}]`)}},v=(t,r)=>{let e=new Set,n=s=>{let u=[s];for(e.add(s);u.length>0;){let i=u.shift();t(i);let m=c(i)||[];for(let L of m)e.has(L.label)||(e.add(L.label),u.push(L.label))}};if(r!==void 0){n(r);return}let a=o.keys();for(let s of a)e.has(s)||n(s)},y=(t,r)=>{let e=new Set,n=s=>{let u=[s];for(e.add(s);u.length>0;){let i=u.pop();t(i);let m=c(i)||[];for(let L of m)e.has(L.label)||(e.add(L.label),u.push(L.label))}};if(r!==void 0){n(r);return}let a=o.keys();for(let s of a)e.has(s)||n(s)},w=(t,r)=>{let e=new Set,n=s=>{if(e.has(s))return;e.add(s),t(s);let u=c(s);if(u)for(let i of u)n(i.label)};if(r!==void 0){n(r);return}let a=o.keys();for(let s of a)n(s)},b=()=>{let t=new Set,r=n=>{let a=[n];for(t.add(n);a.length>0;){let s=a.pop(),u=c(s)||[];for(let i of u){if(i.label!==s&&t.has(i.label))return!0;t.add(i.label),a.push(i.label)}}return!1},e=o.keys();for(let n of e)if(!t.has(n)&&r(n))return!0;return!1},d=()=>{let t=new Set,r=n=>{let a=[n];for(t.add(n);a.length>0;){let s=a.pop(),u=c(s)||[];for(let i of u){if(t.has(i.label))return!0;t.add(i.label),a.push(i.label)}}return!1},e=o.keys();for(let n of e)if(!t.has(n)&&r(n))return!0;return!1},h=()=>l.isDirected?d():b();return(()=>{if(!l.initial)return;let t=Object.keys(l.initial);for(let r of t){let e=l.initial[r]||[];o.set(r,e)}})(),{addVertex:f,getVertex:c,addEdge:g,printGraph:p,bfs:v,dfs:y,dfsRecursive:w,hasCycle:h}};var M=l=>{var o,f;if(l.initial){let c=l.initial.length,g=c>0?l.initial[0].length:0;return[c,g]}return[(o=l.rowsCount)!=null?o:0,(f=l.columnsCount)!=null?f:0]},G=l=>{let[o,f]=M(l),c=Array(o),g=()=>c,p=(b,d,h)=>{c[b][d]=h,l.isDirected||(c[d][b]=h)},v=()=>{for(let b=0;b<o;b++)console.log(c[b].map(d=>d==null?"-":d).join(" "))},y=b=>{let d=[[0,-1],[0,1],[-1,0],[1,0]],h=[[0,0]],t=[];for(let e=0;e<o;e++)t[e]=[];let r=(e,n)=>e>=0&&e<o&&n>=0&&n<f&&!t[e][n];for(;h.length>0;){let[e,n]=h.shift();if(!t[e][n]){t[e][n]=!0,b(e,n,c[e][n]);for(let a=0;a<d.length;a++){let[s,u]=d[a],i=e+s,m=n+u;r(i,m)&&h.push([i,m])}}}},w=b=>{let d=[[0,-1],[0,1],[-1,0],[1,0]],h=[[0,0]],t=[];for(let e=0;e<o;e++)t[e]=[];let r=(e,n)=>e>=0&&e<o&&n>=0&&n<f&&!t[e][n];for(;h.length>0;){let[e,n]=h.pop();if(!t[e][n]){t[e][n]=!0,b(e,n,c[e][n]);for(let a=0;a<d.length;a++){let[s,u]=d[a],i=e+s,m=n+u;r(i,m)&&h.push([i,m])}}}};return(()=>{if(l.initial){c=l.initial;return}for(let b=0;b<o;b++)c[b]=Array(f),l.defaultValue!==void 0&&c[b].fill(l.defaultValue)})(),{getMatrix:g,addEdge:p,printGraph:v,bfs:y,dfs:w}};0&&(module.exports={graph,matrix});
Loading

0 comments on commit 54bf1b5

Please sign in to comment.