{"id":134,"date":"2011-11-09T15:40:32","date_gmt":"2011-11-09T09:40:32","guid":{"rendered":"http:\/\/ujjalruet.wordpress.com\/?p=134"},"modified":"2011-11-09T15:40:32","modified_gmt":"2011-11-09T09:40:32","slug":"minimum-spanning-tree-in-java","status":"publish","type":"post","link":"https:\/\/blog.ujjal.net\/?p=134","title":{"rendered":"Minimum Spanning Tree in JAVA"},"content":{"rendered":"<p>\u09ac\u09a8\u09cd\u09a7\u09c1\u09b0\u09be \u0985\u09a8\u09c7\u0995 \u0986\u0997\u09c7 \u09a5\u09c7\u0995\u09c7 \u09ab\u09cd\u09b0\u09bf\u09b2\u09be\u09a8\u09cd\u09b8\u09bf\u0982 \u0995\u09b0\u09b2\u09c7\u0993 \u0986\u09ae\u09bf \u0995\u09b0\u09bf \u09a8\u09be\u0987\u0964 \u0986\u09ae\u09be\u09b0 \u0987\u099a\u09cd\u099b\u09be \u099b\u09bf\u09b2 \u0986\u0997\u09c7 \u09aa\u09cd\u09b0\u09cb\u09ab\u09be\u0987\u09b2 \u09ad\u09be\u09b2 \u0995\u09b0\u09ac \u09af\u09be\u09a4\u09c7 \u09ac\u09bf\u09a1 \u0995\u09b0\u09b2\u09c7\u0987 \u0995\u09be\u099c \u09aa\u09be\u0987\u0964 \u0997\u09a4 \u0995\u09be\u09b2 \u09b0\u09be\u09a4\u09c7 \u09b9\u099f\u09be\u09a4 \u09ac\u09cd\u09b0\u09be\u0993\u099c\u09be\u09b0 \u098f\u09b0 \u09ac\u09c1\u0995\u09ae\u09be\u09b0\u09cd\u0995 \u09a6\u09c7\u0996\u09a4\u09c7 \u09a6\u09c7\u0996\u09a4\u09c7 \u09b9\u099f\u09be\u09a4 \u099a\u09cb\u0996 \u09aa\u09dc\u09b2\u09cb Ezdia \u09a8\u09be\u09ae\u0995 \u098f\u0995\u099f\u09bf \u09b8\u09be\u0987\u099f \u098f\u0964 \u09a2\u09c1\u0995\u09b2\u09be\u09ae \u0964 \u09aa\u09cd\u09b0\u09a5\u09ae\u09c7\u0987 \u09af\u09be\u09b0 \u0993\u09aa\u09b0 \u099a\u09cb\u0996 \u09aa\u09dc\u09b2 \u09a4\u09be \u09b9\u09b2 minimum spanning tree \u098f\u09b0 implementation JAVA \u09a6\u09bf\u09df\u09c7 \u0995\u09b0\u09a4\u09c7 \u09b9\u09ac\u09c7\u0964 \u09ad\u09be\u09ac\u09b2\u09be\u09ae \u0995\u09cb\u09a1 \u09a4\u09cb C++ \u098f \u0995\u09b0\u09be\u0987 \u0986\u09b8\u09c7\u0964JAVA \u09a4\u09c7 convert \u0995\u09b0\u09a4\u09c7 \u09b0 \u0995\u09a4 \u09b8\u09ae\u09df \u09b2\u09be\u0997\u09ac\u09c7\u0964 Bid \u0995\u09b0\u09b2\u09be\u09ae \u0964 \u0995\u09be\u099c \u099f\u09be \u09aa\u09c7\u09df\u09c7\u0993 \u0997\u09c7\u09b2\u09be\u09ae\u0964 JAVA \u0985\u09a8\u09c7\u0995\u09a6\u09bf\u09a8 \u09a6\u09c7\u0996\u09bf \u09a8\u09be\u0987\u0964\u09a4\u09be\u0987 \u09ad\u09c1\u09b2\u09c7 \u0997\u09c7\u09b8\u09bf\u09b2\u09be\u09ae \u0995\u09bf\u099b\u09c1 \u099c\u09bf\u09a8\u09bf\u09b8\u0964 \u09a4\u09be\u0987 \u0995\u09b0\u09a4\u09c7 \u0995\u09bf\u099b\u09c1\u099f\u09be \u09b8\u09ae\u09df \u09ac\u09c7\u09b8\u09bf \u09b2\u09be\u0997\u09b2\u0964 \u09b8\u09ac \u09ae\u09bf\u09b2\u09bf\u09df\u09c7 \u09e8.\u09eb\u09e6 \u0998\u09a3\u09cd\u099f\u09be\u09df \u0995\u09be\u099c \u099f\u09be delivery \u09a6\u09bf\u09b2\u09be\u09ae\u0964<\/p>\n<p>\u09af\u09be\u0987 \u09b9\u09cb\u0995,\u0995\u09be\u099c\u099f\u09be client \u098f\u09b0 \u09aa\u099b\u09a8\u09cd\u09a6\u0993 \u09b9\u09b2\u0964 \u0995\u09bf\u09a8\u09cd\u09a4\u09c1 \u098f\u0996\u09a8 \u09aa\u09b0\u09cd\u09af\u09a8\u09cd\u09a4 \u0989\u09a8\u09bf \u0986\u09ae\u09be\u0995\u09c7 pay \u0995\u09b0\u09c7\u09a8 \u09a8\u09be\u0987\u0964 \u09aa\u09cd\u09b0\u09a5\u09ae \u0995\u09be\u099c\u09c7\u0987 \u09a7\u09b0\u09be \u0996\u09be\u0987\u09b2\u09be\u09ae\u0964 \u09ac\u09cd\u09af\u09be\u09aa\u09be\u09b0 \u09a8\u09be\u0964 \u0986\u09b0\u0993 \u09b9\u09ac\u09c7\u0964 \u0986\u09aa\u09be\u09a4\u09a4 \u09b8\u09c7\u0987 \u0995\u09be\u099c \u09a4\u09be\u0987 \u0986\u09aa\u09a8\u09be\u0995\u09c7 \u09a6\u09c7\u0996\u09be\u099a\u09cd\u099b\u09bf\u0964 <\/p>\n<p><strong>updater after 2 days<\/strong><br \/>\n\u09a6\u09c7\u09b0\u09c0\u09a4\u09c7 \u09b9\u09b2\u09c7\u0993 \u09ab\u09bf\u09a1\u09ac\u09cd\u09af\u09be\u0995 \u09aa\u09be\u0993\u09df\u09be \u0997\u09c7\u09b2\u0964\u099f\u09be\u0995\u09be\u0993\u0964<\/p>\n<blockquote><p>\nHere is the input\/output format<\/p>\n<p>Input<\/p>\n<p>Input starts with an integer T, denoting the number of test cases.<\/p>\n<p>Each case begins with  an integer n denoting the number of nodes. Then there will be n lines, each having n space separated integers, denoting the lengths of two connected nodes. Each length will be between 0 and 100.<\/p>\n<p>Output:<br \/>\nIf generating a tree is impossible ,then output is -1 else the minimal cost.<\/p>\n<p>Example:<\/p>\n<p>Input:<br \/>\n3<\/p>\n<p>2<br \/>\n27 26<br \/>\n1 52<\/p>\n<p>4<br \/>\n0 10 10 0<br \/>\n0 0 1 1<br \/>\n0 0 0 2<br \/>\n0 0 0 0<\/p>\n<p>4<br \/>\n0 1 0 0<br \/>\n1 0 0 0<br \/>\n0 0 0 1<br \/>\n0 0 1 0<\/p>\n<p>Output:<br \/>\nCase 1: 105<br \/>\nCase 2: 12<br \/>\nCase 3: -1<\/p><\/blockquote>\n<p>[code]<\/p>\n<p>import java.util.*;<\/p>\n<p>class edge {<\/p>\n<p>    public int dist;<br \/>\n    int st, end;<\/p>\n<p>    edge(int i, int j, int k) {<br \/>\n        this.st = i;<br \/>\n        this.end = j;<br \/>\n        this.dist = k;<br \/>\n    }<br \/>\n};<\/p>\n<p>class exe {<\/p>\n<p>    public edge road[] = new edge[3000];<br \/>\n    edge ce;<br \/>\n    int arr[][] = new int[52][52];<br \/>\n    int rank[] = new int[52];<br \/>\n    int par[] = new int[52];<br \/>\n    double ans = 0;<br \/>\n    int min1 = 0, max1 = 0;<br \/>\n    int cost;<br \/>\n    int C, R, cnt, E;<\/p>\n<p>    int find(int i) {<br \/>\n        if (i != par[i]) {<br \/>\n            par[i] = find(par[par[i]]);<br \/>\n        }<br \/>\n        return par[i];<br \/>\n    }<\/p>\n<p>    void link(int x, int y) {<br \/>\n        if (rank[x] &gt; rank[y]) {<br \/>\n            par[y] = x;<br \/>\n        } else {<br \/>\n            par[x] = y;<br \/>\n            if (rank[x] == rank[y]) {<br \/>\n                rank[y]++;<br \/>\n            }<br \/>\n        }<\/p>\n<p>    }<\/p>\n<p>    void kruskal() {<br \/>\n        int i = 0, j = 0, u, v, flag = 0;<br \/>\n        System.out.println(&quot;Tree diagram is &quot;);<br \/>\n        for (i = 0; j &lt; C &#8211; 1 &amp;&amp; i &lt; E; i++) {<br \/>\n            u = find(road[i].st);<br \/>\n            v = find(road[i].end);<br \/>\n            if (u == v) {<br \/>\n                continue;<br \/>\n            }<br \/>\n            link(u, v);<br \/>\n            flag = 1;<br \/>\n            j++;<br \/>\n            min1 += road[i].dist;<\/p>\n<p>            System.out.println(road[i].st + &quot;-&gt;&quot; + road[i].end + &quot;    cost is &quot; + road[i].dist);<\/p>\n<p>        }<br \/>\n        if (j &lt; C &#8211; 1) {<br \/>\n            System.out.println(-1);<br \/>\n        } else {<br \/>\n            \/\/System.out.println(max1-min1);<br \/>\n            System.out.println(&quot;Minimal Cost is &quot; + min1);<br \/>\n        }<\/p>\n<p>    } \/\/krus<\/p>\n<p>    public exe() {<\/p>\n<p>        int i, j, k, T, cs = 0;<\/p>\n<p>        Scanner s = new Scanner(System.in);<\/p>\n<p>        T = s.nextInt();<\/p>\n<p>        while (true) {<br \/>\n            if (T == 0) {<br \/>\n                break;<br \/>\n            }<\/p>\n<p>            C = s.nextInt();<\/p>\n<p>            for (i = 1; i &lt;= C; i++) {<br \/>\n                par[i] = i;<br \/>\n                rank[i] = 0;<br \/>\n            }<\/p>\n<p>            k = 0;<br \/>\n            max1 = 0;<br \/>\n            for (i = 1; i &lt;= C; i++) {<br \/>\n                for (j = 1; j &lt;= C; j++) {<br \/>\n                    arr[i][j] = s.nextInt();<\/p>\n<p>                    if (arr[i][j] != 0 &amp;&amp; i != j) {<br \/>\n                        road[k] = new edge(i, j, arr[i][j]);<br \/>\n                        k++;<br \/>\n                    }<br \/>\n                    max1 += arr[i][j];<br \/>\n                }<br \/>\n            }<\/p>\n<p>            E = k;<br \/>\n            for (i = 0; i &lt; E &#8211; 1; i++) {<br \/>\n                for (j = i + 1; j &lt; E; j++) {<br \/>\n                    if (road[i].dist &gt; road[j].dist) {<br \/>\n                        ce = road[i];<br \/>\n                        road[i] = road[j];<br \/>\n                        road[j] = ce;<br \/>\n                    }<\/p>\n<p>                }<br \/>\n            }<\/p>\n<p>            min1 = 0;<\/p>\n<p>            System.out.print(&quot;Case &quot; + (++cs) + &quot;: &quot;);<br \/>\n            kruskal();<\/p>\n<p>            T&#8211;;<br \/>\n        } \/\/ while<\/p>\n<p>    }<br \/>\n}\/\/class exe<\/p>\n<p>public class Ezdia1 {<\/p>\n<p>    public static void main(String ars[]) {<br \/>\n        try {<br \/>\n            exe object = new exe();<br \/>\n        } catch (Exception p) {<br \/>\n        }<\/p>\n<p>    }<br \/>\n    \/\/main<br \/>\n}\/\/class<\/p>\n<p>[\/code]<\/p>\n<p>\u09a7\u09a8\u09cd\u09af\u09ac\u09be\u09a6 \u09b8\u09ac\u09be\u0987\u0995\u09c7\u0964<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u09ac\u09a8\u09cd\u09a7\u09c1\u09b0\u09be \u0985\u09a8\u09c7\u0995 \u0986\u0997\u09c7 \u09a5\u09c7\u0995\u09c7 \u09ab\u09cd\u09b0\u09bf\u09b2\u09be\u09a8\u09cd\u09b8\u09bf\u0982 \u0995\u09b0\u09b2\u09c7\u0993 \u0986\u09ae\u09bf \u0995\u09b0\u09bf \u09a8\u09be\u0987\u0964 \u0986\u09ae\u09be\u09b0 \u0987\u099a\u09cd\u099b\u09be \u099b\u09bf\u09b2 \u0986\u0997\u09c7 \u09aa\u09cd\u09b0\u09cb\u09ab\u09be\u0987\u09b2 \u09ad\u09be\u09b2 \u0995\u09b0\u09ac \u09af\u09be\u09a4\u09c7 \u09ac\u09bf\u09a1 \u0995\u09b0\u09b2\u09c7\u0987 \u0995\u09be\u099c \u09aa\u09be\u0987\u0964 \u0997\u09a4 \u0995\u09be\u09b2 \u09b0\u09be\u09a4\u09c7 \u09b9\u099f\u09be\u09a4 \u09ac\u09cd\u09b0\u09be\u0993\u099c\u09be\u09b0 \u098f\u09b0 \u09ac\u09c1\u0995\u09ae\u09be\u09b0\u09cd\u0995 \u09a6\u09c7\u0996\u09a4\u09c7 \u09a6\u09c7\u0996\u09a4\u09c7 \u09b9\u099f\u09be\u09a4 \u099a\u09cb\u0996 \u09aa\u09dc\u09b2\u09cb Ezdia \u09a8\u09be\u09ae\u0995 \u098f\u0995\u099f\u09bf \u09b8\u09be\u0987\u099f \u098f\u0964 \u09a2\u09c1\u0995\u09b2\u09be\u09ae \u0964 \u09aa\u09cd\u09b0\u09a5\u09ae\u09c7\u0987 \u09af\u09be\u09b0 \u0993\u09aa\u09b0 \u099a\u09cb\u0996 \u09aa\u09dc\u09b2 \u09a4\u09be \u09b9\u09b2 minimum spanning tree \u098f\u09b0 implementation JAVA \u09a6\u09bf\u09df\u09c7 \u0995\u09b0\u09a4\u09c7 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.ujjal.net\/?p=134\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Minimum Spanning Tree in JAVA&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,10],"tags":[],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-programming-with-java","category-tutorial"],"_links":{"self":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=134"}],"version-history":[{"count":0,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/134\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}