Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Graf = function (wierzcholki, krawedzie, stopnie) {
- var that = this;
- that.wierzcholki = wierzcholki;
- that.stopnie = stopnie
- that.macierzKrawedzi = krawedzie;
- var graf = {
- dodajKrawedz: function (n, m) { //dodanie krzywej
- if (n != m) {
- that.macierzKrawedzi[n][m] = 1;
- that.macierzKrawedzi[m][n] = 1;
- } else {
- console.log('Nie można dodać połączenia do tego samego siebie');
- }
- },
- maKrawedz: function(n,m){
- return that.macierzKrawedzi[n][m] === 1 && that.macierzKrawedzi[m][n] === 1;
- }, // n < m !
- usunKrawedz: function(n, m){
- that.macierzKrawedzi[n][m] = 0;
- that.macierzKrawedzi[m][n] = 0;
- that.stopnie[n]--;
- that.stopnie[m]--;
- if(that.stopnie[m] <= 0) {
- this.usunWierzcholek(m);
- }
- if(that.stopnie[n] <= 0) {
- this.usunWierzcholek(n);
- }
- },
- usunWierzcholek: function(n){
- var indexToRemove = that.wierzcholki.indexOf(n);
- that.stopnie.splice(indexToRemove, 1);
- that.wierzcholki.splice(indexToRemove, 1);
- for(var i=0; i<that.macierzKrawedzi.length; i++){
- that.macierzKrawedzi[i].splice(n, 1);
- }
- that.macierzKrawedzi.splice(n, 1);
- },
- rysujKrawedzie: function(){
- console.table(that.macierzKrawedzi);
- },
- pokazStopnie: function(){
- console.table(that.stopnie);
- },
- wierzcholki: function(){
- return that.wierzcholki.length;
- }
- };
- return graf;
- }
- var a = Graf(
- [0,1,2,3,4,5],
- [
- [0,1,0,0,0,0],
- [1,0,1,1,0,0],
- [0,1,0,0,0,0],
- [0,1,0,0,1,1],
- [0,0,0,1,0,0],
- [0,0,0,1,0,0]
- ],
- [1,3,1,3,1,1]
- );
- var randomMinCut = function(g){
- var i = 0;
- var G = [g];
- while(G[i].wierzcholki() > 2){
- var krawedz = randomNumber(0, G[i].wierzcholki()-1);
- G.push(clone(G[i]));
- console.log("KRAWEDZ DO USUNIECIA ", krawedz, " KRAWEDZIE W GRAFIE ", G[i+1].rysujKrawedzie())
- G[i+1].usunKrawedz(krawedz)
- i++;
- }
- console.table(G);
- }
- // n,m inclusive
- var randomNumber = function(n,m){
- return Math.floor((Math.random() * m) + n);
- }
- var clone = function(obj) {
- if (null == obj || "object" != typeof obj) return obj;
- var copy = obj.constructor();
- for (var attr in obj) {
- if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
- }
- return copy;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement