JavaScript 中的匹配算法

2024-04-05

我正在寻找 JavaScript 的 Blossom 算法的实现或类似的东西。

我有一套对

A - B

A - C

B - D

我需要挑选对,假设每个字母只能在输出中出现一次。在上述情况下,正确的结果是

A - C

B - D

因为 A、B、C 和 D 都最终出现在结果中。错误的结果是

A - B

这会将 C 和 D 排除在外。


当然可以,为什么不呢?

/*
  Edmonds's maximum matching algorithm
  Complexity: O(v^3)
  Written by Felipe Lopes de Freitas
  Adapted to JavaScript from C++ (http://pastebin.com/NQwxv32y) by גלעד ברקן
*/

var MAX = 100,
    undef = -2,
    empty = -1,
    noEdge = 0,
    unmatched = 1,
    matched = 2, 
    forward = 0,
    reverse = 0;

                                 //Labels are the key to this implementation of the algorithm.
function Label(){                //An even label in a vertex means there's an alternating path
       this.even = undefined;    //of even length starting from the root node that ends on the
       this.odd = new Array(2);  //vertex. To find this path, the backtrace() function is called,
};                               //constructing the path by following the content of the labels.
                                 //Odd labels are similar, the only difference is that base nodes
                                 //of blossoms inside other blossoms may have two. More on this later.

function elem(){               //This is the element of the queue of labels to be analyzed by
       this.vertex = undefined;
       this.type = undefined;  //the augmentMatching() procedure. Each element contains the vertex
};                             //where the label is and its type, odd or even.

var g = new Array(MAX);         //The graph, as an adjacency matrix.
for (var i=0; i<MAX; i++){
  g[i] = new Array(MAX);
}
                              //blossom[i] contains the base node of the blossom the vertex i
var blossom = new Array(MAX); //is in. This, together with labels eliminates the need to
                              //contract the graph.

                              //The path arrays are where the backtrace() routine will
var path = new Array(2);
for (var i=0; i<2; i++){
  path[i] = new Array(MAX);
}
var endPath = new Array(2);   //store the paths it finds. Only two paths need to be
                              //stored. endPath[p] denotes the end of path[p].
var match = new Array(MAX);  //An array of flags. match[i] stores if vertex i is in the matching.
                  //label[i] contains the label assigned to vertex i. It may be undefined,
var label = new Array(MAX); //empty (meaning the node is a root) and a node might have even and odd
                  //labels at the same time, which is the case for nonbase nodes of blossoms
for (var i=0; i<MAX; i++){
  label[i] = new Label();
}
var queue = new Array(2*MAX);         //The queue is necessary for efficiently scanning all labels.
var queueFront,queueBack;  //A label is enqueued when assigned and dequeued after scanned.
for (var i=0; i<2*MAX; i++){
  queue[i] = new elem();
}

function initGraph(n){
     for (var i=0; i<n; i++)
         for (var j=0; j<n; j++) g[i][j]=noEdge;
}

function readGraph(){

    var n = graph.n,
        e = graph.e;

    //int n,e,a,b;
    //scanf(" %d %d",&n,&e);      //The graph is read and its edges are unmatched by default.
     initGraph(n);               //Since C++ arrays are 0..n-1 and input 1..n , subtractions 
     for (var i=0; i<e; i++){    //are made for better memory usage.
         //scanf(" %d %d",&a,&b);
         var a = graph[i][0],
             b = graph[i][1];
         if (a!=b)
            g[a-1][b-1]=g[b-1][a-1]=unmatched;
     }
     return n;
}

function initAlg(n){             //Initializes all data structures for the augmentMatching()
     queueFront=queueBack=0;     //function begin. At the start, all labels are undefined,
     for (var i=0; i<n; i++){    //the queue is empty and a node alone is its own blossom.
         blossom[i]=i;
         label[i].even=label[i].odd[0]=label[i].odd[1]=undef;
     }
}

function backtrace (vert, pathNum, stop, parity, direction){
     if (vert==stop) return;           //pathNum is the number of the path to store
     else if (parity==0){              //vert and parity determine the label to be read.
        if (direction==reverse){
           backtrace(label[vert].even,pathNum,stop,(parity+1)%2,reverse);
           path[pathNum][endPath[pathNum]++]=vert;
        }                             //forward means the vertices called first enter
        else if (direction==forward){ //the path first, reverse is the opposite.
             path[pathNum][endPath[pathNum]++]=vert;
             backtrace(label[vert].even,pathNum,stop,(parity+1)%2,forward);
        }
     }
     /*
       stop is the stopping condition for the recursion.
       Recursion is necessary because of the possible dual odd labels.
       having empty at stop means the recursion will only stop after
       the whole tree has been climbed. If assigned to a vertex, it'll stop
       once it's reached.
     */
     else if (parity==1 && label[vert].odd[1]==undef){
        if (direction==reverse){
           backtrace(label[vert].odd[0],pathNum,stop,(parity+1)%2,reverse);
           path[pathNum][endPath[pathNum]++]=vert;
        }
        else if (direction==forward){
             path[pathNum][endPath[pathNum]++]=vert;
             backtrace(label[vert].odd[0],pathNum,stop,(parity+1)%2,forward);
        }
     }
     /*
       Dual odd labels are interpreted as follows:
       There exists an odd length alternating path starting from the root to this
       vertex. To find this path, backtrace from odd[0] to the top of the tree and
       from odd[1] to the vertex itself. This, put in the right order, will
       constitute said path.
     */
     else if (parity==1 && label[vert].odd[1]!=undef){
          if (direction==reverse){
             backtrace(label[vert].odd[0],pathNum,empty,(parity+1)%2,reverse);
             backtrace(label[vert].odd[1],pathNum,vert,(parity+1)%2,forward);
             path[pathNum][endPath[pathNum]++]=vert;
          }
          else if (direction==forward){
               backtrace(label[vert].odd[1],pathNum,vert,(parity+1)%2,reverse);
               backtrace(label[vert].odd[0],pathNum,empty,(parity+1)%2,forward);
               path[pathNum][endPath[pathNum]++]=vert;
          }
     }
}

function enqueue (vert, t){
     var tmp = new elem();               //Enqueues labels for scanning.
     tmp.vertex=vert;        //No label that's dequeued during the execution
     tmp.type=t;             //of augmentMatching() goes back to the queue.
     queue[queueBack++]=tmp; //Thus, circular arrays are unnecessary.
}

function newBlossom (a, b){     //newBlossom() will be called after the paths are evaluated.
     var i,base,innerBlossom,innerBase;
     for (i=0; path[0][i]==path[1][i]; i++);   //Find the lowest common ancestor of a and b
     i--;                                      //it will be used to represent the blossom.
     base=blossom[path[0][i]];                 //Unless it's already contained in another...
                                               //In this case, all will be put in the older one.
     for (var j=i; j<endPath[0]; j++) blossom[path[0][j]]=base;
     for (var j=i+1; j<endPath[1]; j++) blossom[path[1][j]]=base; //Set all nodes to this
     for (var p=0; p<2; p++){                                     //new blossom.
        for (var j=i+1; j<endPath[p]-1; j++){
            if (label[path[p][j]].even==undef){        //Now, new labels will be applied
               label[path[p][j]].even=path[p][j+1];    //to indicate the existence of even
               enqueue(path[p][j],0);                  //and odd length paths.
            }
            else if (label[path[p][j]].odd[0]==undef && label[path[p][j+1]].even==undef){
                 label[path[p][j]].odd[0]=path[p][j+1];
                 enqueue(path[p][j],1);                 //Labels will only be put if the vertex
            }                                           //doesn't have one.

            else if (label[path[p][j]].odd[0]==undef && label[path[p][j+1]].even!=undef){
                 /*
                   If a vertex doesn't have an odd label, but the next one in the path
                   has an even label, it means that the current vertex is the base node
                   of a previous blossom and the next one is contained within it.
                   The standard labeling procedure will fail in this case. This is fixed
                   by going to the last node in the path inside this inner blossom and using
                   it to apply the dual label.
                   Refer to backtrace() to know how the path will be built.
                 */
                 innerBlossom=blossom[path[p][j]];
                 innerBase=j;
                 for (; blossom[j]==innerBlossom && j<endPath[p]-1; j++);
                 j--;
                 label[path[p][innerBase]].odd[0]=path[p][j+1];
                 label[path[p][innerBase]].odd[1]=path[p][j];
                 enqueue(path[p][innerBase],1);
            }
        }
     }
     if (g[a][b]==unmatched){           //All nodes have received labels, except
        if (label[a].odd[0]==undef){    //the ones that called the function in
           label[a].odd[0]=b;           //the first place. It's possible to
           enqueue(a,1);                //find out how to label them by
        }                               //analyzing if they're in the matching.
        if (label[b].odd[0]==undef){
           label[b].odd[0]=a;
           enqueue(b,1);
        }                               
     }
     else if (g[a][b]==matched){
          if (label[a].even==undef){
             label[a].even=b;
             enqueue(a,0);
          }
          if (label[b].even==undef){
             label[b].even=a;
             enqueue(b,0);
          }
     }
}

function augmentPath (){           //An augmenting path has been found in the matching
     var a,b;                  //and is contained in the path arrays.
     for (var p=0; p<2; p++){
         for (var i=0; i<endPath[p]-1; i++){
             a=path[p][i];             //Because of labeling, this path is already
             b=path[p][i+1];           //lifted and can be augmented by simple
             if (g[a][b]==unmatched)   //changing of the matching status.
                g[a][b]=g[b][a]=matched;
             else if (g[a][b]==matched)
                  g[a][b]=g[b][a]=unmatched;
         }
     }
     a=path[0][endPath[0]-1];
     b=path[1][endPath[1]-1];
     if (g[a][b]==unmatched) g[a][b]=g[b][a]=matched;
     else if (g[a][b]==matched) g[a][b]=g[b][a]=unmatched;
     //After this, a and b are included in the matching.
     match[path[0][0]]=match[path[1][0]]=true;
}

function augmentMatching (n){  //The main analyzing function, with the
     var node,nodeLabel;       //goal of finding augmenting paths or
     initAlg(n);               //concluding that the matching is maximum.
     for (var i=0; i<n; i++) if (!match[i]){
         label[i].even=empty;
         enqueue(i,0);          //Initialize the queue with the exposed vertices,
     }                          //making them the roots in the forest.

     while (queueFront<queueBack){
         node=queue[queueFront].vertex;
         nodeLabel=queue[queueFront].type;
         if (nodeLabel==0){
            for (var i=0; i<n; i++) if (g[node][i]==unmatched){
                if (blossom[node]==blossom[i]);
                //Do nothing. Edges inside the same blossom have no meaning.
                else if (label[i].even!=undef){
                     /*
                       The tree has reached a vertex with a label.
                       The parity of this label indicates that an odd length
                       alternating path has been found. If this path is between
                       roots, we have an augmenting path, else there's an
                       alternating cycle, a blossom.
                     */
                     endPath[0]=endPath[1]=0;
                     backtrace(node,0,empty,0,reverse);
                     backtrace(i,1,empty,0,reverse);
                     //Call the backtracing function to find out.
                     if (path[0][0]==path[1][0]) newBlossom(node,i);
                     /*
                       If the same root node is reached, a blossom was found.
                       Start the labelling procedure to create pseudo-contraction.
                     */
                     else {
                          augmentPath();
                          return true;
                          /*
                            If the roots are different, we have an augmenting path.
                            Improve the matching by augmenting this path.
                            Now some labels might make no sense, stop the function,
                            returning that it was successful in improving.
                          */
                     }
                }
                else if (label[i].even==undef && label[i].odd[0]==undef){
                     //If an unseen vertex is found, report the existing path
                     //by labeling it accordingly.
                     label[i].odd[0]=node;
                     enqueue(i,1);
                }
            }
         }
         else if (nodeLabel==1){ //Similar to above.
            for (var i=0; i<n; i++) if (g[node][i]==matched){
                if (blossom[node]==blossom[i]);
                else if (label[i].odd[0]!=undef){
                     endPath[0]=endPath[1]=0;
                     backtrace(node,0,empty,1,reverse);
                     backtrace(i,1,empty,1,reverse);
                     if (path[0][0]==path[1][0]) newBlossom(node,i);
                     else {
                          augmentPath();
                          return true;
                     }
                }
                else if (label[i].even==undef && label[i].odd[0]==undef){
                     label[i].even=node;
                     enqueue(i,0);
                }
            }
         }
         /*
           The scanning of this label is complete, dequeue it and
           keep going to the next one.
         */
         queueFront++;
     }
     /*
       If the function reaches this point, the queue is empty, all
       labels have been scanned. The algorithm couldn't find an augmenting
       path. Therefore, it concludes the matching is maximum.
     */
     return false;
}

function findMaximumMatching (n){
     //Initialize it with the empty matching.
     for (var i=0; i<n; i++) match[i]=false;
     //Run augmentMatching(), it'll keep improving the matching.
     //Eventually, it will no longer find a path and break the loop,
     //at this point, the current matching is maximum.
     while (augmentMatching(n));
}

function main(){
    var n;
    n=readGraph();
    findMaximumMatching(n);
    for (var i=0; i<n; i++){
        for (var j=i+1; j<n; j++) if (g[i][j]==matched)
            console.log(i+1,j+1);
    }
    return 0;
}

Output:

var graph = [[1,2]
            ,[1,3]
            ,[2,4]];

graph["n"] = 4;
graph["e"] = 3;

main()

1 3
2 4
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JavaScript 中的匹配算法 的相关文章

  • 使用 vscode 调试器调试 next.js

    我已经使用安装了一个项目创建下一个应用程序 https github com segmentio create next app 我需要使用我的编辑器 vscode 调试服务器端渲染 所以我访问过vscode recipes 如何调试 ne
  • React js Stripe 结账不起作用

    我正在尝试在 React js 应用程序中呈现条带结账默认表单
  • 使用模数按字母顺序对列表进行排序

    我在获取元素列表并按字母顺序对它们进行排序方面没有任何问题 但我很难理解如何使用模数来做到这一点 更新 这是按我的方式工作的代码 但是 我更喜欢下面提供的答案的可重用性 因此接受了该答案
  • 使用 JavaScript 使链接保持活动状态并在单击时显示悬停效果

    I am struggling to make this work I d like to make it where if O F is clicked the hover state stays active if another li
  • 如何实现n个元素的查找和插入操作的动态二分查找

    这个想法是使用多个数组 每个长度为 2 k 根据 n 的二进制表示来存储 n 个元素 每个数组都是排序的 不同的数组没有以任何方式排序 在上述数据结构中 SEARCH是通过对每个数组进行一系列二分查找来进行的 INSERT 是通过一系列相同
  • 如何防止 Iframe 在与浏览器交互后弄乱浏览器的历史记录?

    因此 就我而言 我使用 Iframe 将 Grafana 附加到我的页面 这为我提供了漂亮且易于使用的图表 可以注意到 每次在图表上进行放大或缩小 使用鼠标单击 交互后 Grafana 的 Iframe 都会在我的 Angular 页面上触
  • Meteor:应用程序无法在 0.9.1.1 版本上运行

    出现类似错误 Error TypeError undefined is not a function evaluating Template create anonymous function iron dynamic template j
  • Google App Engine:修改云运行环境

    我正在尝试部署一个使用自定义 Node js 服务器的 Next js 应用程序 我想将自定义构建变量注入应用程序 next config js const NODE ENV process env NODE ENV const envTy
  • Node.js:如何在检索数据(块)时关闭响应/请求

    我正在用 node js 构建一个应用程序 它加载多个页面并分析内容 因为 node js 发送块 所以我可以分析这些块 如果一个块包含例如索引 nofollow 我想关闭该连接并继续其余部分 var host example com to
  • MVC 在布局代码之前执行视图代码并破坏我的脚本顺序

    我正在尝试将所有 javascript 包含内容移至页面底部 我正在将 MVC 与 Razor 一起使用 我编写了一个辅助方法来注册脚本 它按注册顺序保留脚本 并排除重复的内容 Html RegisterScript scripts som
  • 在javascript中解析json - 长数字被四舍五入

    我需要解析一个包含长数字的 json 在 java servlet 中生成 问题是长数字被四舍五入 当执行这段代码时 var s x 6855337641038665531 var obj JSON parse s alert obj x
  • 将div设置为隐藏,延时后可见

    我试图在 X 时间后 也许甚至在随机时间之后 但现在我们只做固定时间 在黑色背景上出现一个黄色方块 function initialSetup if document getElementById yellow null document
  • Babel 7 Jest Core JS“TypeError:wks不是函数”

    将我的项目升级到 Babel 7 后 通过 Jest 运行测试会抛出以下错误 测试在 Babel 6 中运行没有任何问题 但在 Babel 7 中失败并出现以下错误 TypeError wks is not a function at Ob
  • 如何使输入字段和提交按钮变灰

    我想变灰这两件事 http doorsplit heroku com 歌曲输入字段和提交按钮 直到用户输入艺术家 有没有一种简单的方法可以通过 JQuery 来做到这一点 艺术家输入字段的id是 request artist 你可以这样做
  • 提交表单并重定向页面

    我在 SO 上看到了很多与此相关的其他问题 但没有一个对我有用 我正在尝试提交POST表单 然后将用户重定向到另一个页面 但我无法同时实现这两种情况 我可以获取重定向或帖子 但不能同时获取两者 这是我现在所拥有的
  • Grails 在 javascript 内的 GSP 站点中使用 grails var

    我有一个在 GSP 文件中的 javascript 代码中使用 grails 变量值的问题 例如 我有一个会话值session getAttribute selectedValue 我想在 javascript 代码部分使用这个值 我现在的
  • Angular 2+ 安全性;保护服务器上的延迟加载模块

    我有一个 Angular 2 应用程序 用户可以在其中输入个人数据 该数据在应用程序的另一部分进行分析 该部分仅适用于具有特定权限的人员 问题是我们不想让未经授权的人知道how我们正在分析这些数据 因此 如果他们能够在应用程序中查看模板 那
  • Javascript 数组到 VBScript

    我有一个使用 Javascript 构建的对象数组 我需要使用 VBScript 读取它 如下例所示 我找不到在 VbScript 代码中循环遍历数组的方法myArray object 这个例子是我的问题的简化 我无法更改页面的默认语言 这
  • 确定一组日期的事件重复模式

    我正在寻找一种模式 算法或库 它将采用一组日期并在退出时返回重复的描述 即集合 11 01 2010 11 08 2010 11 15 2010 11 22 2010 11 29 2010 会产生类似 十一月的每个星期一 的结果 有没有人以
  • 如何获取给定 DOM 元素的所有定义的 CSS 选择器?

    如何使用 jQuery 获取给定 DOM 元素的所有定义的 CSS 选择器 定义后 我的意思是在应用于任何样式表的所有 CSS 选择器document 在某种程度上 这类似于 FireBug 实现的功能 其中显示所选 DOM 元素的所有应用

随机推荐