{"id":1138,"date":"2025-11-13T02:49:36","date_gmt":"2025-11-12T18:49:36","guid":{"rendered":"https:\/\/vm1.go2see.me\/?p=1138"},"modified":"2025-11-13T02:49:36","modified_gmt":"2025-11-12T18:49:36","slug":"ask-graph","status":"publish","type":"post","link":"https:\/\/vm1.go2see.me\/?p=1138","title":{"rendered":"ask graph"},"content":{"rendered":"<p>\u82e5\u6709 Alogorithm class \u662f\u5426\u5c31\u4e0d\u5fc5  graphEx\uff1f<\/p>\n<h6>ChatGPT \u8aaa\uff1a<\/h6>\n<p>\u662f\u7684\uff0c\u5982\u679c\u4f60\u7684\u5c08\u6848\u4e2d\u5df2\u7d93\u6709\u5b98\u65b9\u8981\u6c42\u7684 <code>Algorithms<\/code> \u985e\u5225\uff08\u4f8b\u5982 <code>Algorithms.partitionGraph(Graph g, int numberOfPartitions)<\/code>\uff09\uff0c\u90a3\u9ebc\u5c31<strong>\u4e0d\u4e00\u5b9a\u9700\u8981\u984d\u5916\u5efa\u7acb <code>GraphEx<\/code><\/strong> \u985e\u5225\u3002<\/p>\n<p>\u6839\u64da MP2 \u6587\u4ef6\u8aaa\u660e\uff0c<code>Algorithms<\/code> \u985e\u662f\u5c08\u9580\u8a2d\u8a08\u4f86\u653e\u5404\u7a2e\u8207\u5716\u904b\u7b97\u6709\u95dc\u7684<strong>\u975c\u614b\u6f14\u7b97\u6cd5<\/strong>\u7684\u5730\u65b9\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><code>partitionGraph<\/code> \u7528\u4f86\u6839\u64da\u8ddd\u96e2\u8207\u9130\u8fd1\u689d\u4ef6\u5206\u5272\u5716\uff1b<\/li>\n<li>\u5b83\u5fc5\u9808<strong>\u56de\u50b3\u5206\u5272\u5f8c\u7684\u5b50\u5716\u96c6\u5408\uff08induced subgraphs\uff09<\/strong>MP2-Operations and Applications\u2026\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u2705 \u82e5\u5df2\u6709 <code>Algorithms<\/code> \u985e\uff0c\u5efa\u8b70\u7684\u8a2d\u8a08\u7b56\u7565<\/h3>\n<p>\u4f60\u53ef\u4ee5\u628a\u6240\u6709\u300c\u4e0d\u5c6c\u65bc\u5716\u672c\u8eab\u7d50\u69cb\u3001\u4f46\u8981\u64cd\u4f5c\u5716\u7684\u6f14\u7b97\u6cd5\u300d\u90fd\u96c6\u4e2d\u5230 <code>Algorithms<\/code> \u985e\u4e2d\uff1a<\/p>\n<pre><code>public final class Algorithms {\n\n    private Algorithms() { }\n\n    public static &lt;V extends Vertex, E extends Edge&lt;V&gt;&gt;\n    List&lt;V&gt; minimumCostPath(Graph&lt;V,E&gt; g, V s, V t, PathCostType type) { ... }\n\n    public static &lt;V extends Vertex, E extends Edge&lt;V&gt;&gt;\n    double pathCost(Graph&lt;V,E&gt; g, List&lt;V&gt; path, PathCostType type) { ... }\n\n    public static &lt;V extends Vertex, E extends Edge&lt;V&gt;&gt;\n    double diameter(Graph&lt;V,E&gt; g, PathCostType type) { ... }\n\n    public static &lt;V extends Vertex, E extends Edge&lt;V&gt;&gt;\n    V center(Graph&lt;V,E&gt; g, PathCostType type) { ... }\n\n    public static &lt;V extends Vertex, E extends Edge&lt;V&gt;&gt;\n    List&lt;Graph&lt;V,E&gt;&gt; partitionGraph(Graph&lt;V,E&gt; g, int k) { ... }\n}<\/code><\/pre>\n<hr \/>\n<h3>\ud83d\udcd8 \u4f55\u6642\u4ecd\u5efa\u8b70\u4fdd\u7559 <code>GraphEx<\/code><\/h3>\n<p>\u82e5\u4f60\u7684 <code>Algorithms<\/code> \u985e\u5df2\u7d93\u5f88\u5927\u3001\u6216\u88ab\u898f\u7bc4\u70ba\u53ea\u5be6\u4f5c Task 5\uff08<code>partitionGraph<\/code>\uff09\uff0c<br \/>\n\u800c\u4f60\u53c8\u60f3\u5c07 Task 3\u30014 \u7684\u8def\u5f91\u8207\u4e2d\u5fc3\u8a08\u7b97\u62bd\u96e2\u5171\u7528\u908f\u8f2f\uff0c<br \/>\n\u90a3\u9ebc\u53ef\u4ee5\u8b93 <code>GraphEx<\/code> \u4f5c\u70ba<strong>\u5167\u90e8\u5de5\u5177\u5c64<\/strong>\uff1a<\/p>\n<ul>\n<li><code>GraphEx<\/code> \u5c08\u6ce8\u65bc\u300c\u6838\u5fc3\u6f14\u7b97\u6cd5\u300d\uff08Dijkstra\u3001BFS\u3001components\uff09\u3002<\/li>\n<li><code>Algorithms<\/code> \u53ea\u662f\u4e00\u500b\u516c\u958b API\uff0c\u5167\u90e8\u547c\u53eb <code>GraphEx<\/code>\u3002<\/li>\n<\/ul>\n<p>\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>public static &lt;V extends Vertex, E extends Edge&lt;V&gt;&gt;\nList&lt;Graph&lt;V,E&gt;&gt; partitionGraph(Graph&lt;V,E&gt; g, int k) {\n    return GraphEx.inducedSubgraphs(g, GraphEx.computePartitions(g, k));\n}<\/code><\/pre>\n<hr \/>\n<h3>\ud83e\udded \u7e3d\u7d50<\/h3>\n<ul>\n<li>\u2705 <strong>\u53ea\u6709 <code>Algorithms<\/code> \u4e00\u500b\u985e\u5225\u4e5f\u53ef\u4ee5\u5b8c\u6210\u6240\u6709\u5de5\u4f5c<\/strong>\uff0c\u4e0d\u5fc5\u5f37\u5236\u518d\u5efa <code>GraphEx<\/code>\u3002<\/li>\n<li>\ud83e\udde9 \u4f46\u82e5\u4f60\u5e0c\u671b\u7a0b\u5f0f\u7d50\u69cb\u66f4\u6e05\u6670\u3001\u6f14\u7b97\u6cd5\u53ef\u91cd\u7528\uff08\u4f8b\u5982 <code>minimumCostPath<\/code>\u3001<code>getDiameter<\/code> \u6703\u88ab\u591a\u500b\u985e\u5171\u7528\uff09\uff0c<br \/>\n\u5c31\u53ef\u4fdd\u7559 <code>GraphEx<\/code> \u4f5c\u70ba\u5167\u90e8\u8f14\u52a9\u985e\u3002<\/li>\n<li>\u26a0\ufe0f \u7121\u8ad6\u54ea\u7a2e\u65b9\u5f0f\uff0c\u8a18\u5f97<strong>\u4e0d\u8981\u6539\u52d5 Graph.java \/ Vertex.java \/ Edge.java<\/strong>MP2-Operations and Applications\u2026\u3002<\/li>\n<\/ul>\n<p>\u8cc7\u6599\u4f86\u6e90<\/p>\n<h5>\u4f60\u8aaa\uff1a<\/h5>\n<p>\u6c92\u6709 add \u5982\u4f55\u5efa\u7acb AdjacencyMatrixGraph <\/p>\n<h6>ChatGPT \u8aaa\uff1a<\/h6>\n<p>\u6839\u64da MP2 \u4efb\u52d9\u6587\u4ef6\u7684\u8aaa\u660e\uff0c<strong>AdjacencyMatrixGraph<\/strong> \u88ab\u8a2d\u8a08\u6210\u300c\u4e0d\u53ef\u8b8a\u5716\uff08immutable graph\uff09\u300d\u2014\u2014\u4e5f\u5c31\u662f\u8aaa\u5b83<strong>\u6c92\u6709 <code>addVertex()<\/code> \u6216 <code>addEdge()<\/code> \u65b9\u6cd5<\/strong>\uff0c\u800c\u662f\u5fc5\u9808\u5728<strong>\u5efa\u69cb\u6642\uff08constructor\uff09\u4e00\u6b21\u6027\u5efa\u7acb\u597d\u6574\u5f35\u5716<\/strong>MP2-Operations and Applications\u2026\u3002<\/p>\n<hr \/>\n<h3>\u2705 \u6b63\u78ba\u5efa\u7acb\u65b9\u5f0f<\/h3>\n<p>\u5728\u6c92\u6709 <code>add<\/code> \u65b9\u6cd5\u7684\u60c5\u6cc1\u4e0b\uff0c\u6700\u5408\u7406\u7684\u505a\u6cd5\u662f\u8b93 <code>AdjacencyMatrixGraph<\/code> \u5177\u5099\u4e00\u500b<strong>\u5efa\u69cb\u5b50\u6216\u975c\u614b\u5de5\u5ee0\u65b9\u6cd5<\/strong>\uff0c\u76f4\u63a5\u5f9e\uff1a<\/p>\n<ol>\n<li><strong>\u9802\u9ede\u96c6\u5408<\/strong> \u548c <strong>\u908a\u96c6\u5408<\/strong><\/li>\n<li>\u6216\u8005\u5f9e <strong>\u53e6\u4e00\u500b\u5df2\u7d93\u69cb\u5efa\u597d\u7684 Graph \u7269\u4ef6<\/strong><\/li>\n<\/ol>\n<p>\u5efa\u7acb\u77e9\u9663\u7d50\u69cb\u3002<\/p>\n<p>\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>public final class AdjacencyMatrixGraph&lt;V extends Vertex, E extends Edge&lt;V&gt;&gt;\n        implements Graph&lt;V, E&gt; {\n\n    private final List&lt;V&gt; vertices;\n    private final double[][] matrix;\n\n    \/** \u5efa\u69cb\u4e00\u500b\u4e0d\u53ef\u8b8a\u7684\u5716 *\/\n    public AdjacencyMatrixGraph(Set&lt;V&gt; vertices, Set&lt;E&gt; edges) {\n        this.vertices = List.copyOf(vertices);\n        int n = this.vertices.size();\n        this.matrix = new double[n][n];\n\n        \/\/ \u521d\u59cb\u5316\u70ba\u7121\u9650\u8ddd\u96e2\n        for (int i = 0; i &lt; n; i++) {\n            Arrays.fill(matrix[i], Double.POSITIVE_INFINITY);\n            matrix[i][i] = 0;\n        }\n\n        \/\/ \u586b\u5165\u908a\u6b0a\u91cd\n        for (E edge : edges) {\n            int i = this.vertices.indexOf(edge.v1());\n            int j = this.vertices.indexOf(edge.v2());\n            double w = edge.length();\n            matrix[i][j] = w;\n            matrix[j][i] = w; \/\/ \u7121\u5411\u5716\n        }\n    }\n\n    \/** \u5f9e\u53e6\u4e00\u500b Graph \u8907\u88fd\u70ba\u77e9\u9663\u578b *\/\n    public static &lt;V extends Vertex, E extends Edge&lt;V&gt;&gt;\n    AdjacencyMatrixGraph&lt;V, E&gt; fromGraph(Graph&lt;V, E&gt; g) {\n        return new AdjacencyMatrixGraph&lt;&gt;(g.getVertices(), g.getEdges());\n    }\n}<\/code><\/pre>\n<hr \/>\n<h3>\ud83e\udde9 \u5be6\u4f5c\u8981\u9ede<\/h3>\n<ul>\n<li>\n<p><code>AdjacencyMatrixGraph<\/code> \u53ea\u9700\u63d0\u4f9b\u89c0\u5bdf\u65b9\u6cd5\uff08<code>getEdge(v1, v2)<\/code>, <code>hasVertex(v)<\/code>, <code>edgeLength(v1, v2)<\/code> \u7b49\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u82e5\u8981\u7522\u751f\u8a98\u5c0e\u5b50\u5716\uff08induced subgraph\uff09\uff0c\u53ef\u7528\u975c\u614b\u65b9\u6cd5\uff1a<\/p>\n<pre><code>public AdjacencyMatrixGraph<V,E> inducedSubgraph(Set<V> subset)<\/code><\/pre>\n<p>\u5b83\u6703\u5f9e\u539f\u77e9\u9663\u4e2d\u62bd\u53d6\u5c0d\u61c9\u9802\u9ede\u8207\u908a\u751f\u6210\u65b0\u5716\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>\ud83d\udeab \u4e0d\u5efa\u8b70\u7684\u4f5c\u6cd5<\/h3>\n<p>\u4e0d\u8981\u5617\u8a66\u8b93\u77e9\u9663\u7248\u5716\u652f\u63f4 <code>add<\/code> \u6216 <code>remove<\/code>\uff1a<\/p>\n<ul>\n<li>\u9019\u6703\u7834\u58de\u300c\u4e0d\u53ef\u8b8a\u300d\u7684\u8a2d\u8a08\u539f\u5247\uff1b<\/li>\n<li>\u4e26\u9055\u53cd MP2 \u7684\u4efb\u52d9\u8aaa\u660e\uff0c\u660e\u78ba\u6307\u51fa\u300cmutators \u53ea\u9069\u7528\u65bc AdjacencyListGraph\u300dMP2-Operations and Applications\u2026\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\ud83d\udd1a \u7e3d\u7d50<\/h3>\n<blockquote>\n<p>\u82e5\u6c92\u6709 <code>addVertex<\/code>\/<code>addEdge<\/code>\uff0c\u8acb\u5728\u5efa\u69cb\u5b50\u4e2d\u4e00\u6b21\u6027\u63a5\u6536 <code>Set&lt;V&gt;<\/code> \u548c <code>Set&lt;E&gt;<\/code> \u6216\u7528 <code>fromGraph()<\/code> \u8f49\u63db\u65b9\u5f0f\u5efa\u7acb\u3002<br \/>\n\u9019\u6a23\u7b26\u5408 MP2 \u7684\u300cAdjacencyMatrixGraph \u70ba\u4e0d\u53ef\u8b8a\u5716\u300d\u898f\u7bc4\uff0c\u4e14\u80fd\u6b63\u78ba\u53c3\u8207 Task 3\u20135 \u7684\u6f14\u7b97\u6cd5\u904b\u7b97\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u82e5\u6709 Alogorithm class \u662f\u5426\u5c31\u4e0d\u5fc5 graphEx\uff1f ChatGPT \u8aaa\uff1a \u662f\u7684\uff0c\u5982\u679c\u4f60\u7684\u5c08\u6848&#8230; &raquo; <a class=\"read-more-link\" href=\"https:\/\/vm1.go2see.me\/?p=1138\">\u95b1\u8b80\u5168\u6587<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1138","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/1138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1138"}],"version-history":[{"count":2,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/1138\/revisions"}],"predecessor-version":[{"id":1140,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/1138\/revisions\/1140"}],"wp:attachment":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}