{"format":"brushup-mindmap-v1","id":"ds-06-graph","title":"图【王道】","structure":"logic-right","theme":"auto","root":{"id":"ae683acd3d588eede17fbb5e50","topic":"第六章 图","expanded":true,"children":[{"id":"f8cae7f975e88cf79bea78894e","topic":"图的定义及基本术语","expanded":true,"children":[{"id":"5bab21d6-f907-48df-8036-b3087badb529","topic":"概念","expanded":true,"children":[{"id":"89605663-e993-42a5-8da1-f98ac67ebf1f","topic":"图G由顶点集V和边集E组成，记G=(V, E)，其中V(G)表示图G中顶点的有限非空集；E(G)表示图G中顶点之间的关系（边）集合","expanded":true}]},{"id":"ad2cc0e3-3bbe-48e0-ad7f-c77a9c946220","topic":"基本术语","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"486e9402-5b13-4515-9df9-7289a943a1ba","topic":"关系及路径术语","expanded":true,"children":[{"id":"8fe45fde-8dad-41a7-99f1-a38bf388b059","topic":"邻接：有边/弧相连的两个顶点之间的关系（无向图的边：(vi, vj)，有向图的边：<vi, vj>）","expanded":true},{"id":"67bc9e10-5ea3-42dc-9114-cd54d52caa45","topic":"关联（依附）：边/弧与顶点之间的关系","expanded":true},{"id":"93f039de-cb79-4c9e-85bd-5eb43fec24a7","topic":"顶点的度：与该顶点相关联的边的数目，记为TD(v)\n有向图中，顶点的度等于该顶点的入度和出度之和","expanded":true},{"id":"fdb8829a-81c4-45f5-bda1-0cdd7486fcd5","topic":"路径：连续的边构成的顶点序列\n路径长度：路径上边或弧的数目/权值之和","expanded":true},{"id":"985c2b45-48f3-41cb-a16b-12bd58e8a9e9","topic":"回路（环）：第一个顶点和最后一个顶点相同的路径","expanded":true},{"id":"a87af467-7c40-4d8f-899d-d640e9f7b1d4","topic":"简单路径：除路径起点和终点可以相同外，其余顶点均不相同的路径\n简单回路（简单环）：除路径起点和终点相同外，其余顶点均不相同的路径","expanded":true}]},{"id":"7778db72-2c08-47be-87fa-867e702f84f1","topic":"图的术语","expanded":true,"style":{"branchColor":"#EA8ABA"},"children":[{"id":"893cb226-ff8e-4b65-9965-55746fa53b1b","topic":"无向图：每条边都是无方向的\n有向图：每条边都是有方向的","expanded":true},{"id":"7e0b6aa5-fb69-4cba-8a8f-35309b9f59b2","topic":"无向完全图：任意两个点都有一条边\n有向完全图：任意两个点都有两条边","expanded":true},{"id":"5357df37-58e8-4bb3-8d77-42062e10d2eb","topic":"简单图：不存在重复边，不存在顶点到自身的边\n多重图：反之","expanded":true},{"id":"60ee6748-0f85-4e8f-a895-f94f9dc100c1","topic":"稀疏图：有很少边或弧的图（e<nlogn）\n稠密图：有较多边或弧的图","expanded":true},{"id":"5551fdea-5370-4ff5-a5cf-056b9f2c4bd1","topic":"子图：设有两个图G=(V, E)和G'=(V', E')，若V'是V的子集，且E'是E的子集，则称G'是G的子图","expanded":true},{"id":"ff53b3e2-f4a8-44a6-bac2-ed040022d955","topic":"网：边/弧带权的图","expanded":true},{"id":"65e36992-47e0-4abd-b0e6-2064327e14a4","topic":"连通图（强连通图）：在无（有）向图中，若对任何两个顶点v，u都存在从v到u的路径，则G是连通图（强连通图）","expanded":true,"style":{"borderColor":"#EB4D3D"}}]},{"id":"1bbdc66f-00dc-4599-9f5b-317dc3f4428b","topic":"子图相关术语","expanded":true,"children":[{"id":"8abce36b-225e-4c3d-be81-2026720522f0","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-c1dbaab43f51fa.png","width":301.47,"height":208.06},"children":[{"id":"f395b722-8037-4015-aadb-d3490be455bf","topic":"连通分量/极大连通子图：该子图是G连通子图，将G的任何不在该子图的顶点加入，子图不再连通\n强连通分量/极大强连通子图：该子图是G强连通子图，将G的任何不在该子图的顶点加入，子图不再强连通","expanded":true},{"id":"25f29512-9cd2-470a-8734-be87a9271b2e","topic":"极小连通子图：该子图是G的连通子图，在该子图中删除任何一条边，子图不再连通","expanded":true},{"id":"e8c0b496-c8ff-4561-8df5-fc79b5c40aec","topic":"生成树：包含无向图G所有顶点的极小连通子图","expanded":true},{"id":"37f79038-3c58-4788-9191-813922f9292c","topic":"生成森林，对非连通图，由各个连通分量的生成树的集合","expanded":true}]}]}]}]},{"id":"4ce97c8b8abb1ed894887a7a44","topic":"图的存储结构","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"d79c07a1-2bd3-4c12-9144-a7d668023d48","topic":"邻接矩阵法","expanded":true,"children":[{"id":"72d68d1c-aeab-4c8f-a7a5-94a969eae782","topic":"概念","expanded":true,"children":[{"id":"8dbde003-a883-4df4-9144-2f68f6deba84","topic":"是指用一个一位数组存储图中顶点的信息，用一个二维数组存储图中边的信息（邻接关系），二维数组即邻接矩阵","expanded":true}]},{"id":"f53b975c-6ccf-4ef6-b4f5-59bf6c5c9cb1","topic":"无向图","expanded":true,"children":[{"id":"66a46dbb-a75f-4012-8dfd-72a51b3a3e29","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-592a1d8c0f5c47.png","width":406.94,"height":113.41},"children":[{"id":"e4cb9f7c-11ba-490c-8850-29e16a019a22","topic":"无向图的邻接矩阵是对称的","expanded":true},{"id":"f8646e7d-d75d-450c-ab3e-10b61b2a2a54","topic":"顶点i的度=第i行（或第i列）中1的个数","expanded":true}]}]},{"id":"28f51971-fe96-4463-9da9-dcfe0c0215a8","topic":"有向图","expanded":true,"children":[{"id":"92b84ea4-6d1e-411c-bc4f-2da175b71a3c","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-a0a4bce5cc2951.png","width":316.64,"height":114.74},"children":[{"id":"c23a9177-3b43-4811-9f1a-fb5feaeb630d","topic":"有向图的邻接矩阵可能是不对称的","expanded":true},{"id":"1dfc3f8f-00d6-4dd3-be0e-fd6516a5d091","topic":"顶点的出度 = 第i行元素之和","expanded":true},{"id":"eb88b93d-563a-4b2d-ab5b-3abe6c347622","topic":"顶点的入度 = 第i列元素之和","expanded":true}]}]},{"id":"67340c21-56f3-4e4c-8c77-57a633048ead","topic":"有向网","expanded":true,"children":[{"id":"bb7e06e1-11ad-44bb-b5e6-db2c6f1e53e5","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-3beea2ba60ff55.png","width":378.67,"height":129.97},"children":[{"id":"dc387efd-09b7-4d74-8545-1fefe083f62b","topic":"arcs[i][j] 定义","expanded":true,"formula":{"tex":"A.\\mathrm{arcs}[i][j] = \\begin{cases} W_{ij}, & \\langle v_i, v_j \\rangle \\text{ 或 } (v_i, v_j) \\in VR \\\\ \\infty, & \\text{无边（弧）} \\end{cases}","displayMode":true,"mathml":"<span class=\"katex\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><mi>A</mi><mi mathvariant=\"normal\">.</mi><mrow><mi mathvariant=\"normal\">a</mi><mi mathvariant=\"normal\">r</mi><mi mathvariant=\"normal\">c</mi><mi mathvariant=\"normal\">s</mi></mrow><mo stretchy=\"false\">[</mo><mi>i</mi><mo stretchy=\"false\">]</mo><mo stretchy=\"false\">[</mo><mi>j</mi><mo stretchy=\"false\">]</mo><mo>=</mo><mrow><mo fence=\"true\">{</mo><mtable rowspacing=\"0.36em\" columnalign=\"left left\" columnspacing=\"1em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><msub><mi>W</mi><mrow><mi>i</mi><mi>j</mi></mrow></msub><mo separator=\"true\">,</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mo stretchy=\"false\">⟨</mo><msub><mi>v</mi><mi>i</mi></msub><mo separator=\"true\">,</mo><msub><mi>v</mi><mi>j</mi></msub><mo stretchy=\"false\">⟩</mo><mtext> 或 </mtext><mo stretchy=\"false\">(</mo><msub><mi>v</mi><mi>i</mi></msub><mo separator=\"true\">,</mo><msub><mi>v</mi><mi>j</mi></msub><mo stretchy=\"false\">)</mo><mo>∈</mo><mi>V</mi><mi>R</mi></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mi mathvariant=\"normal\">∞</mi><mo separator=\"true\">,</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mtext>无边（弧）</mtext></mstyle></mtd></mtr></mtable></mrow></mrow><annotation encoding=\"application/x-tex\">A.\\mathrm{arcs}[i][j] = \\begin{cases} W_{ij}, &amp; \\langle v_i, v_j \\rangle \\text{ 或 } (v_i, v_j) \\in VR \\\\ \\infty, &amp; \\text{无边（弧）} \\end{cases}</annotation></semantics></math></span>"}}]}]},{"id":"3f54b918-ba0c-4e47-9577-d1bab9b1ae09","topic":"缺点","expanded":true,"children":[{"id":"ac4033c4-78d2-47f3-bb05-6e5a06227509","topic":"不便于增加和删除结点、存稀疏图浪费大量空间（O(n^2)）、统计一共有多少条边浪费时间","expanded":true}]},{"id":"99193561-0fae-4595-932c-120176d39ab6","topic":"补充","expanded":true,"children":[{"id":"6b4316c0-6c58-43fd-9047-9dd0017ac4ee","topic":"邻接矩阵A的A^n[i][j]等于由顶点i到j的长度为n的路径数量","expanded":true}]}]},{"id":"2b107b2b-4c79-4c51-9af6-dd73e105f11a","topic":"邻接表法","expanded":true,"style":{"branchColor":"#15831C"},"children":[{"id":"0a64c089-ec0e-454a-ab18-a1ad22a56922","topic":"概念","expanded":true,"children":[{"id":"a0abb712-ba38-4703-af8d-3c49aee7a23e","topic":"图中顶点用一个一维数组存储，元素包含指向第一个邻接点的指针，存储顶点和头指针的一维数组叫顶点表","expanded":true},{"id":"ff8d74bb-100f-432e-8b10-fe145b5c15d1","topic":"每个顶点的所有邻接点构成一个单链表","expanded":true}]},{"id":"d56bfe72-ce7d-4153-b2cf-2650b951bf74","topic":"结构","expanded":true,"children":[{"id":"983f14ad-2c9b-4a23-81e2-7652ea78905b","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-bb603ab7a0d3ac.png","width":439.78,"height":109.3},"children":[{"id":"91de0f3e-a33b-45cb-b1f6-383f78ccb43e","topic":"若为无向图，则需要存储两倍的边，存储空间为O(|V|+2|E|)","expanded":true},{"id":"0ec8ced1-4db6-4431-ba49-6d1ef57470bd","topic":"若为有向图，存储空间为O(|V|+|E|)，若采用邻接表法则找出度易，找入度难；若采用逆邻接表则找入度易，找出度难","expanded":true},{"id":"b1bf7b4c-77cb-42e4-a5d2-b0c749e65947","topic":"有向图邻接表法中，顶点vi出度为第i个单链表中结点个数；入度为整个单链表中邻接点域值是i-1的结点个数","expanded":true},{"id":"c8740dbb-ff3a-4eec-b062-f02990afd018","topic":"图的邻接表表示不唯一","expanded":true,"style":{"borderColor":"#EB4D3D"}}]}]},{"id":"4ca987db-3bad-4c99-8d5f-9d1e9b0cae9e","topic":"实现","expanded":true,"children":[{"id":"23ec0821-1c5b-4e43-ac8a-acc4dae60a57","topic":"VNode / AdjList 定义","expanded":true,"children":[{"id":"b7ebcaa3-10e3-490a-b764-31619ca3738a","topic":"ArcNode 定义","expanded":true,"children":[{"id":"3a691db3-15e7-42ee-a492-49b492a344b4","topic":"顶点表结点：顶点域+边表头指针","expanded":true},{"id":"29cf8ef3-2fdd-4f0b-8acf-67f8d7e51c11","topic":"边表结点：邻接点域+指针域+权值（若为网）","expanded":true}],"code":{"lang":"c","code":"#define MVNum 100           //最大顶点数\ntypedef struct ArcNode{     //边结点\n    int adjvex;             //该边所指向的顶点的位置\n    struct ArcNode * nextarc; //指向下一条边的指针\n    OtherInfo info;         //和边相关的信息\n}ArcNode;","title":"ArcNode","tokens":[[{"text":"#define ","darkColor":"#C699E0"},{"text":"MVNum","darkColor":"#6EC0DF"},{"text":" ","darkColor":"#C699E0"},{"text":"100","darkColor":"#E2B159"},{"text":"           //最大顶点数","darkColor":"#908B7E"}],[{"text":"typedef","darkColor":"#8EA4E2"},{"text":" ","darkColor":"#C699E0"},{"text":"struct","darkColor":"#8EA4E2"},{"text":" ArcNode","darkColor":"#C699E0"},{"text":"{","darkColor":"#BAB5A8"},{"text":"     //边结点","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#C699E0"},{"text":"int","darkColor":"#74CBC4"},{"text":" adjvex","darkColor":"#C699E0"},{"text":";","darkColor":"#BAB5A8"},{"text":"             //该边所指向的顶点的位置","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#C699E0"},{"text":"struct","darkColor":"#8EA4E2"},{"text":" ArcNode ","darkColor":"#C699E0"},{"text":"*","darkColor":"#BAB5A8"},{"text":" nextarc","darkColor":"#C699E0"},{"text":";","darkColor":"#BAB5A8"},{"text":" //指向下一条边的指针","darkColor":"#908B7E"}],[{"text":"    OtherInfo info","darkColor":"#C699E0"},{"text":";","darkColor":"#BAB5A8"},{"text":"         //和边相关的信息","darkColor":"#908B7E"}],[{"text":"}","darkColor":"#BAB5A8"},{"text":"ArcNode","darkColor":"#C699E0"},{"text":";","darkColor":"#BAB5A8"}]]}}],"code":{"lang":"c","code":"typedef struct VNode{\n    VerTexType data;        //顶点信息\n    ArcNode * firstarc;     //指向第一条依附该顶点的边的指针\n}VNode, AdjList[MVNum]; //AdjList表示邻接表类型\n// 说明：例如，AdjList v;   相当于：VNode v[MVNum];","title":"VNode / AdjList","tokens":[[{"text":"typedef","darkColor":"#8EA4E2"},{"text":" ","darkColor":"#DEDBD2"},{"text":"struct","darkColor":"#8EA4E2"},{"text":" VNode","darkColor":"#DEDBD2"},{"text":"{","darkColor":"#BAB5A8"}],[{"text":"    VerTexType data","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"},{"text":"        //顶点信息","darkColor":"#908B7E"}],[{"text":"    ArcNode ","darkColor":"#DEDBD2"},{"text":"*","darkColor":"#BAB5A8"},{"text":" firstarc","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"},{"text":"     //指向第一条依附该顶点的边的指针","darkColor":"#908B7E"}],[{"text":"}","darkColor":"#BAB5A8"},{"text":"VNode","darkColor":"#DEDBD2"},{"text":",","darkColor":"#BAB5A8"},{"text":" ","darkColor":"#DEDBD2"},{"text":"AdjList","darkColor":"#EEECE6"},{"text":"[","darkColor":"#BAB5A8"},{"text":"MVNum","darkColor":"#DEDBD2"},{"text":"];","darkColor":"#BAB5A8"},{"text":" //AdjList表示邻接表类型","darkColor":"#908B7E"}],[{"text":"// 说明：例如，AdjList v;   相当于：VNode v[MVNum];","darkColor":"#908B7E"}]]}}]},{"id":"1357a32e-0af3-4395-8965-555855543c91","topic":"缺点","expanded":true,"children":[{"id":"d50de366-fb41-4239-b2ec-e571f3d502d0","topic":"若不构建逆邻接表则找入度麻烦、不方便检查任意一对顶点之间是否存在边","expanded":true}]}]},{"id":"1fa694b7-8fd2-4a51-9a4e-acff1b6963e9","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-eaf88250e9f382.png","width":479.39,"height":96.41}},{"id":"b5864681-c2f8-439f-a8fd-921214e7036b","topic":"十字链表法（有向图）","expanded":true,"style":{"branchColor":"#15831C"},"children":[{"id":"b4f3a39d-163b-4d08-a28c-0e36b72ef5f9","topic":"概念","expanded":true,"children":[{"id":"c1a54f26-2d8d-45ca-9504-cba946c05050","topic":"十字链表时针对有向图的存储方式，对应于有向图中的每条弧有一个结点，对应于每个顶点也有一个结点（有向图的邻接表与逆邻接表的结合）","expanded":true}]},{"id":"db790a36-2123-4d29-b1f7-72decb0b6d0c","topic":"结构","expanded":true,"children":[{"id":"2089bb46-000b-403d-8803-21e843447714","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-8d9b2e3c66eadf.png","width":514.49,"height":146.63},"children":[{"id":"fd78fed8-fb15-4ebb-996c-d1c59b649629","topic":"顶点结点域结构","expanded":true,"children":[{"id":"8888b26e-9eac-43f3-a156-02b9a7f9d266","topic":"data域（存放顶点数据信息） + firstin域 + firstout域（分别指向以该顶点为弧头或弧尾的第一个弧结点）","expanded":true}]},{"id":"0db57506-1715-4f3e-9e33-5787f3d55163","topic":"弧结点域结构","expanded":true,"children":[{"id":"7bd6b6e5-fb0f-46a1-a651-4046beb6359a","topic":"尾域（弧尾）+ 头域（弧头）+ hlink域（指向弧头相同的下一条弧）+ tlink域（指向弧尾相同的下一条弧）+ info域（弧相关信息）","expanded":true}]}]}]},{"id":"f65c6a0c-ba88-4c6b-a069-bc409d39ab0a","topic":"图的十字链表不唯一，但一个十字链表表示确定一个图","expanded":true}]},{"id":"cf016151-4743-4ec8-8b3e-acb852defef5","topic":"邻接多重表（无向图）","expanded":true,"children":[{"id":"d752a47f-84bf-4c6d-bae4-68aa70166721","topic":"概念","expanded":true,"children":[{"id":"f22cb522-229f-4aab-8178-057be588e025","topic":"对邻接表的边表进行改造，得到专门针对存储无向图的邻接多重表","expanded":true}]},{"id":"80de8627-dd12-4ac0-bdac-49e2013e3f71","topic":"结构","expanded":true,"children":[{"id":"afcef21d-ec8b-4dbd-82c2-8edb8a207308","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-0a1dc8725cd4dd.png","width":454.39,"height":130.74},"children":[{"id":"0ff3f10e-9fd8-4aef-9de1-0935e5fd44ec","topic":"顶点结点域结构","expanded":true,"children":[{"id":"4fe63a68-d258-4d66-8412-c88eec4b5a47","topic":"data域（存储该顶点的信息）+ firstedge域（指示第一条依附于该顶点的边）","expanded":true}]},{"id":"45ea5649-f897-4e6c-8802-53658d3e646a","topic":"弧结点域结构","expanded":true,"children":[{"id":"0460e0cf-027c-4174-82e3-aba2be467c61","topic":"标志域（标记该边是否被搜索过）+ ivex域 + jvex域（该边依附的两个顶点在图中位置）+ ilink域 + jlink域（分别指向下一条依附于顶点ivex、jvex的边）+ info域（弧相关信息）","expanded":true}]}]}]}]}]},{"id":"32a966cf281819b90b78a85497","topic":"图的遍历","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"c5096452-86ea-40db-b25f-3189db695ee2","topic":"广度优先搜索","expanded":true,"children":[{"id":"57c42a35-8eb7-40eb-9dc7-167de953ec27","topic":"概念","expanded":true,"children":[{"id":"217f6f87-43d1-427e-b870-626cdbe308ec","topic":"类似于二叉树的层序遍历算法，多了标记数组，用于确定已访问的结点","expanded":true}]},{"id":"2ee4a1cc-bdd6-4827-9117-c51cb8e7be00","topic":"算法思想","expanded":true,"children":[{"id":"62f69ab9-8ece-456e-88db-35b6524c8a4a","topic":"BFS 代码实现","expanded":true,"children":[{"id":"2f2d728c-044a-41e5-a1be-87a172076d6e","topic":"首先访问起始顶点v（进队列），由v出发，依次访问v的各个未访问过的邻接顶点w1,w2,...,wi（都加入队列，起始顶点pop掉）","expanded":true},{"id":"7642fadc-dfd3-4c4d-bbc6-e4cd13db53d6","topic":"然后依次访问w1,w2,...,wi的所有未访问过的邻接顶点（加入队列，并pop访问过的顶点）","expanded":true},{"id":"02762af0-911c-4290-8c37-a18f4cb6c5f3","topic":"再从这些顶点出发，访问所有未被访问过的邻接顶点，直到遍历完成（利用队列实现）","expanded":true}],"code":{"lang":"c","code":"//广度优先遍历\nvoid BFS(Graph G,int v){    //从顶点v出发，广度优先遍历图G\n    visit(v);               //访问初始顶点v\n    visited[v]=TRUE;        //对v做已访问标记\n    Enqueue(Q,v);           //顶点v入队列Q\n    while(!isEmpty(Q)){\n        DeQueue(Q,v);       //顶点v出队列\n        for(w=FirstNeighbor(G,v);w>=0;w=NextNeighbor(G,v,w))\n            //检测v所有邻接点\n            if(!visited[w]){    //w为v的尚未访问的邻接顶点\n                visit(w);       //访问顶点w\n                visited[w]=TRUE;//对w做已访问标记\n                EnQueue(Q,w);   //顶点w入队列\n            }//if\n    }//while\n}","title":"BFS","tokens":[[{"text":"//广度优先遍历","darkColor":"#908B7E"}],[{"text":"void","darkColor":"#74CBC4"},{"text":" ","darkColor":"#DEDBD2"},{"text":"BFS","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"Graph ","darkColor":"#DEDBD2"},{"text":"G","darkColor":"#D5C5AA"},{"text":",","darkColor":"#BAB5A8"},{"text":"int","darkColor":"#74CBC4"},{"text":" ","darkColor":"#DEDBD2"},{"text":"v","darkColor":"#D5C5AA"},{"text":"){","darkColor":"#BAB5A8"},{"text":"    //从顶点v出发，广度优先遍历图G","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"visit","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"v","darkColor":"#6EC0DF"},{"text":");","darkColor":"#BAB5A8"},{"text":"               //访问初始顶点v","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"visited","darkColor":"#EEECE6"},{"text":"[","darkColor":"#BAB5A8"},{"text":"v","darkColor":"#DEDBD2"},{"text":"]=","darkColor":"#BAB5A8"},{"text":"TRUE","darkColor":"#E2B159"},{"text":";","darkColor":"#BAB5A8"},{"text":"        //对v做已访问标记","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"Enqueue","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"Q","darkColor":"#6EC0DF"},{"text":",","darkColor":"#BAB5A8"},{"text":"v","darkColor":"#6EC0DF"},{"text":");","darkColor":"#BAB5A8"},{"text":"           //顶点v入队列Q","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"while","darkColor":"#8EA4E2"},{"text":"(","darkColor":"#BAB5A8"},{"text":"!","darkColor":"#8EA4E2"},{"text":"isEmpty","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"Q","darkColor":"#6EC0DF"},{"text":")){","darkColor":"#BAB5A8"}],[{"text":"        ","darkColor":"#DEDBD2"},{"text":"DeQueue","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"Q","darkColor":"#6EC0DF"},{"text":",","darkColor":"#BAB5A8"},{"text":"v","darkColor":"#6EC0DF"},{"text":");","darkColor":"#BAB5A8"},{"text":"       //顶点v出队列","darkColor":"#908B7E"}],[{"text":"        ","darkColor":"#DEDBD2"},{"text":"for","darkColor":"#8EA4E2"},{"text":"(","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#DEDBD2"},{"text":"=","darkColor":"#BAB5A8"},{"text":"FirstNeighbor","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"G","darkColor":"#6EC0DF"},{"text":",","darkColor":"#BAB5A8"},{"text":"v","darkColor":"#6EC0DF"},{"text":");","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#DEDBD2"},{"text":">=","darkColor":"#BAB5A8"},{"text":"0","darkColor":"#E2B159"},{"text":";","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#DEDBD2"},{"text":"=","darkColor":"#BAB5A8"},{"text":"NextNeighbor","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"G","darkColor":"#6EC0DF"},{"text":",","darkColor":"#BAB5A8"},{"text":"v","darkColor":"#6EC0DF"},{"text":",","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#6EC0DF"},{"text":"))","darkColor":"#BAB5A8"}],[{"text":"            ","darkColor":"#BAB5A8"},{"text":"//检测v所有邻接点","darkColor":"#908B7E"}],[{"text":"            ","darkColor":"#DEDBD2"},{"text":"if","darkColor":"#8EA4E2"},{"text":"(","darkColor":"#BAB5A8"},{"text":"!","darkColor":"#8EA4E2"},{"text":"visited","darkColor":"#EEECE6"},{"text":"[","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#DEDBD2"},{"text":"]){","darkColor":"#BAB5A8"},{"text":"    //w为v的尚未访问的邻接顶点","darkColor":"#908B7E"}],[{"text":"                ","darkColor":"#DEDBD2"},{"text":"visit","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#6EC0DF"},{"text":");","darkColor":"#BAB5A8"},{"text":"       //访问顶点w","darkColor":"#908B7E"}],[{"text":"                ","darkColor":"#DEDBD2"},{"text":"visited","darkColor":"#EEECE6"},{"text":"[","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#DEDBD2"},{"text":"]=","darkColor":"#BAB5A8"},{"text":"TRUE","darkColor":"#E2B159"},{"text":";","darkColor":"#BAB5A8"},{"text":"//对w做已访问标记","darkColor":"#908B7E"}],[{"text":"                ","darkColor":"#DEDBD2"},{"text":"EnQueue","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"Q","darkColor":"#6EC0DF"},{"text":",","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#6EC0DF"},{"text":");","darkColor":"#BAB5A8"},{"text":"   //顶点w入队列","darkColor":"#908B7E"}],[{"text":"            ","darkColor":"#DEDBD2"},{"text":"}","darkColor":"#BAB5A8"},{"text":"//if","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"}","darkColor":"#BAB5A8"},{"text":"//while","darkColor":"#908B7E"}],[{"text":"}","darkColor":"#BAB5A8"}]]}}]},{"id":"e2d949b6-0659-44ae-b68c-1bdc6503e886","topic":"性能分析","expanded":true,"children":[{"id":"cc249f2b-1a73-4778-a083-730722ecdfa5","topic":"时间复杂度","expanded":true,"children":[{"id":"502a0bcc-e6cd-4d04-82cb-9f502a4e6327","topic":"邻接矩阵存储方式：O(|V|^2)","expanded":true},{"id":"3bedd8cb-74ae-47e9-86e6-dd1eda27f2a7","topic":"邻接表存储方式：O(|V|+|E|)","expanded":true}]},{"id":"4989fb2a-455b-4f44-a1ad-014dfdd1d6bf","topic":"空间复杂度","expanded":true,"children":[{"id":"405b2efe-f6d2-4404-b47d-80301034a51e","topic":"BFS需要借助一个队列，n个顶点均需要入队一次，所以最坏情况下n个顶点在队列，O(|V|)","expanded":true}]}]},{"id":"2f7b59d8-1efa-4eee-957e-169f41da6389","topic":"基于邻接矩阵的遍历BFS序列是唯一的，基于邻接表的遍历所得到的BFS序列是不唯一的","expanded":true}]},{"id":"5774628c-3c0a-420d-a859-0e86d1393bcf","topic":"深度优先搜索","expanded":true,"style":{"branchColor":"#BA69C8"},"children":[{"id":"ad2797da-df73-4eee-abc8-d99187056a8e","topic":"概念","expanded":true,"children":[{"id":"e51c9e48-0a41-4054-8f37-fac66a199347","topic":"类似于树的先序遍历，搜索策略是尽可能深地搜索一个图","expanded":true}]},{"id":"9b34b21e-5c3a-4ff1-9dbc-54a98ec98cea","topic":"算法思想","expanded":true,"children":[{"id":"f9b05fd4-9439-42f7-89f5-70792b5af8e5","topic":"DFS 代码实现","expanded":true,"children":[{"id":"99ba2a6f-d4f8-4380-bd95-27a4eeeed20f","topic":"首先访问图中某个起始顶点v，由v出发，访问与v邻接且未被访问的任一顶点w1，再访问与w1邻接且未访问的任一顶点，重复","expanded":true},{"id":"3ef415d5-5f44-4ceb-be1e-e8aaca0ce8f6","topic":"当不能继续向下访问，则依次退回到最近被访问的顶点，若还有邻接顶点未被访问，则从该点继续DFS，直到所有顶点均被访问为止（用递归实现）","expanded":true}],"code":{"lang":"c","code":"bool visited[MAX_VERTEX_NUM];   //访问标记数组\n\nvoid DFS(Graph G,int v){        //从顶点v出发，深度优先遍历图G\n    visit(v);                   //访问顶点v\n    visited[v]=TRUE;            //设已访问标记\n    for(w=FirstNeighbor(G,v);w>=0;w=NextNeighbor(G,v,w))\n        if(!visited[w]){        //w为u的尚未访问的邻接顶点\n            DFS(G,w);\n        }   //if\n}","title":"DFS","tokens":[[{"text":"bool","darkColor":"#74CBC4"},{"text":" ","darkColor":"#DEDBD2"},{"text":"visited","darkColor":"#EEECE6"},{"text":"[","darkColor":"#BAB5A8"},{"text":"MAX_VERTEX_NUM","darkColor":"#DEDBD2"},{"text":"];","darkColor":"#BAB5A8"},{"text":"   //访问标记数组","darkColor":"#908B7E"}],[{"text":" "}],[{"text":"void","darkColor":"#74CBC4"},{"text":" ","darkColor":"#DEDBD2"},{"text":"DFS","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"Graph ","darkColor":"#DEDBD2"},{"text":"G","darkColor":"#D5C5AA"},{"text":",","darkColor":"#BAB5A8"},{"text":"int","darkColor":"#74CBC4"},{"text":" ","darkColor":"#DEDBD2"},{"text":"v","darkColor":"#D5C5AA"},{"text":"){","darkColor":"#BAB5A8"},{"text":"        //从顶点v出发，深度优先遍历图G","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"visit","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"v","darkColor":"#6EC0DF"},{"text":");","darkColor":"#BAB5A8"},{"text":"                   //访问顶点v","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"visited","darkColor":"#EEECE6"},{"text":"[","darkColor":"#BAB5A8"},{"text":"v","darkColor":"#DEDBD2"},{"text":"]=","darkColor":"#BAB5A8"},{"text":"TRUE","darkColor":"#E2B159"},{"text":";","darkColor":"#BAB5A8"},{"text":"            //设已访问标记","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"for","darkColor":"#8EA4E2"},{"text":"(","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#DEDBD2"},{"text":"=","darkColor":"#BAB5A8"},{"text":"FirstNeighbor","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"G","darkColor":"#6EC0DF"},{"text":",","darkColor":"#BAB5A8"},{"text":"v","darkColor":"#6EC0DF"},{"text":");","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#DEDBD2"},{"text":">=","darkColor":"#BAB5A8"},{"text":"0","darkColor":"#E2B159"},{"text":";","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#DEDBD2"},{"text":"=","darkColor":"#BAB5A8"},{"text":"NextNeighbor","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"G","darkColor":"#6EC0DF"},{"text":",","darkColor":"#BAB5A8"},{"text":"v","darkColor":"#6EC0DF"},{"text":",","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#6EC0DF"},{"text":"))","darkColor":"#BAB5A8"}],[{"text":"        ","darkColor":"#DEDBD2"},{"text":"if","darkColor":"#8EA4E2"},{"text":"(","darkColor":"#BAB5A8"},{"text":"!","darkColor":"#8EA4E2"},{"text":"visited","darkColor":"#EEECE6"},{"text":"[","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#DEDBD2"},{"text":"]){","darkColor":"#BAB5A8"},{"text":"        //w为u的尚未访问的邻接顶点","darkColor":"#908B7E"}],[{"text":"            ","darkColor":"#DEDBD2"},{"text":"DFS","darkColor":"#6EC0DF"},{"text":"(","darkColor":"#BAB5A8"},{"text":"G","darkColor":"#6EC0DF"},{"text":",","darkColor":"#BAB5A8"},{"text":"w","darkColor":"#6EC0DF"},{"text":");","darkColor":"#BAB5A8"}],[{"text":"        ","darkColor":"#DEDBD2"},{"text":"}","darkColor":"#BAB5A8"},{"text":"   //if","darkColor":"#908B7E"}],[{"text":"}","darkColor":"#BAB5A8"}]]}}]},{"id":"183c23c7-33d9-4e01-a87d-693e3a5d4fbf","topic":"性能分析","expanded":true,"children":[{"id":"85023299-dc10-4b45-a9a8-b2c487d02b41","topic":"时间复杂度","expanded":true,"children":[{"id":"03d0308d-de78-49b0-a503-fcfcd65562a1","topic":"邻接矩阵存储方式：O(|V|^2)","expanded":true},{"id":"2ec76a19-8775-4fb5-81c5-08a3eee24525","topic":"邻接表存储方式：O(|V|+|E|)","expanded":true}]},{"id":"d9521edd-c2d3-4c55-b874-141444111b7e","topic":"空间复杂度","expanded":true,"children":[{"id":"4d488e92-8ee9-4f7b-bccd-4e10de454c0f","topic":"DFS是一个递归算法，需要工作栈辅助，最多需要图中所有顶点进栈，O(|V|)","expanded":true}]}]},{"id":"9c4ebbf7-3856-49dd-9744-c21e486e0abd","topic":"基于邻接矩阵的遍历DFS序列是唯一的，基于邻接表的遍历所得到的DFS序列是不唯一的","expanded":true},{"id":"c69b43f2-1fa6-4f0c-9f7d-b2d5348275c4","topic":"输出语句移到退出递归前，实现逆拓扑序列输出","expanded":true,"style":{"borderColor":"#EB4D3D"}}]}]},{"id":"867b1b50defb7f822a31189d56","topic":"图的应用","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"3df52417-e30d-498c-843c-d55abac90c95","topic":"最小生成树","expanded":true,"children":[{"id":"8b1671cb-c43a-4fba-b1b4-93b73abc5254","topic":"概念","expanded":true,"children":[{"id":"e297322b-3b12-4ebc-9ea4-c8f1de66c54a","topic":"生成树：所有顶点均由边连接在一起，但不存在回路的图","expanded":true},{"id":"2094ab6e-98d3-494c-b912-acea94d2b648","topic":"最小生成树：权值之和最小的那棵生成树","expanded":true}]},{"id":"d51b7e23-c1c9-45d2-86ff-01e39176dcfe","topic":"性质","expanded":true,"children":[{"id":"8520de9d-f983-4aad-bf9f-92f8bf99c9dc","topic":"最小生成树不是唯一的","expanded":true},{"id":"6077c702-91a2-4963-a79d-0c3f07957135","topic":"其对应的边的权值之和总是唯一的，且是最小的","expanded":true},{"id":"f12553ae-8403-4363-8986-3f48969b1619","topic":"最小生成树的边数=顶点数-1","expanded":true}]},{"id":"f70b7044-b362-40cb-9416-095f31cb9dd7","topic":"算法","expanded":true,"children":[{"id":"5db9bee1-dcde-48bf-8ee2-4f19dc951cab","topic":"算法基于MST性质","expanded":true,"children":[{"id":"2609c8a5-a63f-453c-bed7-9f0b9642ed35","topic":"性质解释：n个顶点分属已经在生成树上的顶点集U和未在生成树上的顶点集V-U，接下来应在连通U和V-U的边中选取权值最小的边","expanded":true}]},{"id":"07c0aec1-30a8-4b45-83da-06bb97ee1118","topic":"Prim算法","expanded":true,"children":[{"id":"2b7b8618-a82c-4d76-900a-1c0809710042","topic":"概述","expanded":true,"children":[{"id":"3e3621f3-a814-4f88-8938-8dcd172162e5","topic":"每次将代价最小的新顶点加入生成树","expanded":true}]},{"id":"ff6133a9-8b7a-4f97-bb4f-7433510eab4b","topic":"实现","expanded":true,"children":[{"id":"e1a005e7-3e95-4831-ac3c-9ccee3bc1e06","topic":"开始时从图中任取一个顶点加入树T","expanded":true},{"id":"37a32a58-322e-41ac-aa83-8d31f985200b","topic":"之后选择一个与当前T中顶点集合距离最近的顶点，将该点和对应边加入T，每次操作后T中顶点数和边数都+1","expanded":true},{"id":"a71cae18-beb0-47d4-b3ad-c480405ae5cc","topic":"以此类推，当所有点加入T，必然有n-1条边，即T就是最小生成树","expanded":true}]}]},{"id":"682d16ce-4aa8-4f79-becc-e610d8d0a42d","topic":"Kruskal算法","expanded":true,"children":[{"id":"3143227f-6fa2-4e01-b95e-7f13788b975f","topic":"概述","expanded":true,"children":[{"id":"1f1e25c2-d800-4d7a-9de0-b5c7c5e5424a","topic":"每次选一条权值最小的边（边按权值排序），使其连通（用并查集判断并实现）","expanded":true}]},{"id":"8660e430-db1e-4d3a-a55c-de98282c2c63","topic":"实现","expanded":true,"children":[{"id":"494bac24-d3f1-40a1-9772-74b9e0566b6a","topic":"开始时为只有n个顶点而无边的非连通图T={V,{}}，每个顶点自成一个连通分量","expanded":true},{"id":"e0fe555e-de00-4269-a975-f1fd6e911c3a","topic":"然后按照边的权值由小到大，不断选取当前未被选取过且权值最小的边","expanded":true},{"id":"4a9309e4-00f4-4fe9-9d45-b5c1feb9812d","topic":"若该边依附的顶点在两个连通分量上，则将边加入T，否则继续选取下一条边","expanded":true},{"id":"e33aee17-4e0e-4040-be59-021dd69ac8b0","topic":"以此类推，直到T中所有顶点都在一个连通分量上","expanded":true}]}]},{"id":"0463e38e-ee35-4155-8bbb-7156ee360881","topic":"比较","expanded":true,"children":[{"id":"a380eef1-85b2-49e2-84be-0dc353cb0dcc","topic":"算法名","expanded":true,"children":[{"id":"348d142b-da67-4ddf-8e0c-0cd50c8a73a4","topic":"Prim算法","expanded":true,"children":[{"id":"ffbb3df5-e590-4f8e-aa36-dc3b1341dfed","topic":"Kruskal算法","expanded":true}]}]},{"id":"3f535fc3-3ae1-4688-adc2-e5921016d3d5","topic":"算法思想","expanded":true,"children":[{"id":"14ac9a22-0f9a-411f-9cab-55e18fa45de8","topic":"选择点","expanded":true,"children":[{"id":"b583f54e-8e7a-4ffd-93c6-c2de273304e2","topic":"选择边","expanded":true}]}]},{"id":"19fa752e-e10f-4144-8d11-723a0be369c4","topic":"时间复杂度","expanded":true,"children":[{"id":"474fc0bc-1779-4f90-80be-915646cba75b","topic":"O(|V|^2)，V为顶点数","expanded":true,"children":[{"id":"f439c615-5462-44e3-a2e4-ff96046c406a","topic":"O(|E|log|E|)，E为边数","expanded":true}]}]},{"id":"2bbe2c4a-b581-4d32-8cfe-f0a0f4ad0530","topic":"适应范围","expanded":true,"children":[{"id":"2c522063-975c-4a0c-8573-995116b7d163","topic":"稠密图","expanded":true,"children":[{"id":"243d7cc3-77e1-4d71-b366-2f88f37aa4eb","topic":"稀疏图","expanded":true}]}]}]}]}]},{"id":"6045ecae-f1dc-4bb9-9937-4f0f1132f160","topic":"最短路径","expanded":true,"style":{"branchColor":"#EA8ABA"},"children":[{"id":"77908f33-c826-473e-99bb-23df76fe84d8","topic":"在有向网中A点（源点）到达B点（终点）的多条路径中，找到一条各边权值之和最小的路径","expanded":true},{"id":"c2abae6d-9797-467d-92f2-a025e8f6636e","topic":"Dijkstra算法（两点间最短路径）","expanded":true,"children":[{"id":"5ddc00db-4442-4c1f-a3af-ef274be37502","topic":"概述","expanded":true,"children":[{"id":"dc7641be-5ace-451b-a599-03382262750a","topic":"维护一个最短路径数组，每次选取最短的顶点加入，更新加入后的最短路径，直到所有顶点都访问","expanded":true},{"id":"3a66d202-1e14-4db7-9eae-2fe5c3336567","topic":"s[]记录是否访问，dist[]记录源地到各点最短路径，path[]记录前驱结点","expanded":true}]},{"id":"a9f38165-4791-4b58-8607-642f56f70338","topic":"过程","expanded":true,"children":[{"id":"f56d7e82-c80b-450b-a07e-8653acf02936","topic":"初始化：集合S初始为{0}（源点入集合），dist[]初始值dist[i]=arcs[0][i]（与源点距离）","expanded":true},{"id":"95dfe592-b19f-4f0b-9968-0ecdb24ecbc9","topic":"从顶点集合V-S中选出dist[]数组值最小的，即选最近的点加入","expanded":true},{"id":"6af354b8-b314-41d0-99b1-7eb7febe17ba","topic":"修改V0出发到集合V-S上任一顶点最短路径长度：若dist[j]+arcs[j][k]<dist[k]，则更新dist[k]=dist[j]+arcs[j][k]","expanded":true},{"id":"d7f5283d-93ee-4720-83a0-63bba1c7c4c6","topic":"重复步骤2-3，n-1次，直到所有顶点都包含在S中","expanded":true}]},{"id":"14f50175-d44e-4e7c-9f3b-e1c557df02a0","topic":"时间复杂度","expanded":true,"children":[{"id":"4ad1eab7-6c16-4dbc-835c-7e48ba6d107d","topic":"O(|V|^2)","expanded":true}]},{"id":"6bc7dcf3-2d82-4bbe-b603-465b6cf8f117","topic":"注意","expanded":true,"children":[{"id":"7d137217-1b8d-49be-b107-8691f2ea2644","topic":"适合稠密图，无负权值","expanded":true}]}]},{"id":"a754f5e2-ccb2-4756-8532-764845c0ada1","topic":"Floyd算法（各顶点之间最短路径）","expanded":true,"children":[{"id":"2298702f-2382-4a04-b7b5-836a9f9e9370","topic":"概述","expanded":true,"children":[{"id":"6be7c689-fc37-4e1e-a119-87763abe469f","topic":"维护一个各顶点间最短路径二维数组，不断试探加入中间结点，是否缩短距离（三重循环）","expanded":true}]},{"id":"3f3a342b-d0e7-4193-ba37-d8afc0dc4a7c","topic":"过程","expanded":true,"children":[{"id":"847bd133-d539-45c2-87bc-84dc77494eda","topic":"初始化：对任意两个顶点vi和vj，若存在边，则二维数组上最短路径为权值（不存在则最短路径为无穷）","expanded":true},{"id":"5e101802-2024-4bec-9cb7-1bce86d3796a","topic":"逐步尝试在原路径上加入顶点k(k = 0,1,...,n-1)为中间结点","expanded":true},{"id":"3f6e6e4e-1b47-421b-8dbf-c5fe4f7c0e9f","topic":"若更新后得到路径比原本路径长度短，则新路径代替原本路径","expanded":true}]},{"id":"d8dc032a-3534-4ebc-8b59-4ca9bcd87ff5","topic":"时间复杂度","expanded":true,"children":[{"id":"b5b307c4-8579-4852-88e5-6c0d9ebc585a","topic":"O(|V|^3)","expanded":true}]},{"id":"763ce551-9b07-4535-8e42-98fe33be9cb7","topic":"注意","expanded":true,"children":[{"id":"2b8cb6ee-67b3-4d73-914b-6f6f4a551ce1","topic":"允许图中有带父权值的边，但不允许有包含带负权值的边组成回路","expanded":true},{"id":"1953d852-eaf8-4a5e-bb17-0042e7022fb5","topic":"适用于带权无向图","expanded":true}]}]},{"id":"d0fbce37-1cca-445a-a564-64b1e0995b59","topic":"建议理解意思后，直接看下面补充代码更清晰","expanded":true}]},{"id":"5e02c66c-88d6-47fd-b903-9c784b952562","topic":"以下为有向无环图的应用","expanded":true,"children":[{"id":"98c30182-f989-410d-afad-ba68a91ee48f","topic":"有向无环图：无环的有向图，简称DAG图","expanded":true},{"id":"3b6df775-eafa-41c5-9174-fb1b532d763c","topic":"用一个有向图表示一个工程的各子工程及其互相制约关系","expanded":true,"children":[{"id":"1199c8be-793f-437e-ad6e-07d50d21d75d","topic":"AOV网：顶点表示活动，弧表示活动之间的优先制约关系","expanded":true},{"id":"346fa41c-5972-44c0-968c-37c1046676e7","topic":"AOE网：弧表示活动，以顶点表示活动的开始或结束事件","expanded":true}]}]},{"id":"60c27679-8716-4068-90db-a39803d05c1e","topic":"有向无环图描述表达式","expanded":true,"style":{"branchColor":"#EA8ABA"},"children":[{"id":"c16cc946-09ad-4fce-ba97-13edfbb69010","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-bdfeb2f30098c3.png","width":339.62,"height":144.39},"children":[{"id":"06364489-1b41-4be5-87cf-88759b8caef0","topic":"6.4.28题为例","expanded":true,"children":[{"id":"5adb7d2c-8b92-4488-87d6-1fd112916b5d","topic":"操作数在最下层排成一排","expanded":true},{"id":"d1acf310-0bff-4ad9-bdd9-53983032ca91","topic":"按生效顺序，加入运算符（分层）","expanded":true},{"id":"c4e9cbec-5d85-4fe2-80b4-6f9d1e7d09a3","topic":"从底向上检查同层能否合并","expanded":true}]}]}]},{"id":"248cb0c1-3b11-4803-8586-e15115d34b29","topic":"拓扑排序（AOV网）","expanded":true,"children":[{"id":"b0c7d298-ee4c-40a2-8e59-186460f0de03","topic":"概述","expanded":true,"children":[{"id":"250c0ccc-bd28-4557-a9b9-bbc80367558b","topic":"拓扑序列","expanded":true,"children":[{"id":"fbc10154-8dd9-4802-b48f-4016b51ca13c","topic":"拓扑序列是对图中所有的顶点，如果存在一条从顶点A到顶点B的路径，那么在排序中顶点A出现在顶点B的前面","expanded":true}]},{"id":"bcf0d96d-b9cb-46d5-bcf9-419f9f9e2d4b","topic":"拓扑排序","expanded":true,"children":[{"id":"d8e23920-ac4c-48e1-9668-3df465ba5b9d","topic":"对一个有向图构造拓扑序列的过程","expanded":true}]}]},{"id":"509a4f82-a437-4fcb-80c4-e68e818482e5","topic":"过程","expanded":true,"children":[{"id":"f0951220-4b6e-4985-8938-8553555ac307","topic":"从AOV网中选择一个没有前驱的顶点并输出","expanded":true},{"id":"27fa75a5-d133-4a81-8bc4-c651fa468914","topic":"从网中删除该顶点和所有以它为起点的有向边","expanded":true},{"id":"dd6b9a2b-d60d-4ce5-b159-9de5c884ed6b","topic":"重复前两个步骤，直到当前的AOV网为空或当前网中不存在无前驱的顶点为止（后者说明有向图中存在环）","expanded":true}]},{"id":"8cd9ab49-0fd9-481b-a607-3f89c6bfe613","topic":"时间复杂度","expanded":true,"children":[{"id":"fc7d9f35-e9a4-4521-b7de-14763f0f6f74","topic":"邻接矩阵存储，O(|V|^2)","expanded":true},{"id":"a29fafae-3003-4e88-ac82-004d11878b86","topic":"邻接表存储，O(|V|+|E|)","expanded":true}]},{"id":"159c1656-f853-4689-ae2b-41e15366e24c","topic":"注意","expanded":true,"children":[{"id":"38f754a6-b8de-4354-8d4b-63f1da93703a","topic":"若一个顶点有多个直接后继，则拓扑排序通常不唯一（若每个顶点有唯一前驱后继，则唯一）","expanded":true},{"id":"ec609937-6018-41e9-9b0f-676d4f5c579e","topic":"若图的邻接矩阵是三角矩阵，则存在拓扑排序，反之不一定成立","expanded":true}]}]},{"id":"2fcfa3db-f459-4860-9bfd-3c1d467eeff2","topic":"逆拓扑排序（AOV网）","expanded":true,"style":{"branchColor":"#EA8ABA"},"children":[{"id":"57c9d9ea-b4d9-4974-9f18-c7a1dde185db","topic":"实现","expanded":true,"children":[{"id":"fcb12972-ec7c-4519-85f8-7d20a4f7ae4d","topic":"DFS算法","expanded":true}]},{"id":"1a2e6db9-4c7e-4969-9c94-b1bb4123b421","topic":"过程","expanded":true,"children":[{"id":"4bdf3bf6-5d4b-4ed9-8161-a92d3c6de948","topic":"从AOV网中选择一个没有后继的顶点并输出","expanded":true},{"id":"8a4c36a9-ed6a-4dc0-b1b0-a17a82f522fe","topic":"从网中删除该顶点和所有以它为终点的有向边","expanded":true},{"id":"dc147b20-3369-43ba-ad27-60bfa2c0f133","topic":"重复前两个步骤，直到当前的AOV网为空","expanded":true}]}]},{"id":"56fc973d-e364-47c2-955b-c1cf49288af1","topic":"关键路径（AOE网）","expanded":true,"children":[{"id":"b5861715-6f5f-49c0-a46b-ffd2e8962b03","topic":"概述","expanded":true,"children":[{"id":"f57d9c5e-5363-4d63-84d4-abbb55824d45","topic":"关键路径","expanded":true,"children":[{"id":"ed4a87b9-3ed3-492f-8da3-90730214a875","topic":"从源点到汇点的所有路径中，具有最大路径长度的路径","expanded":true}]},{"id":"6b9132b6-8838-45ba-9b64-30c57dc12f93","topic":"关键活动","expanded":true,"children":[{"id":"57240508-9ecd-41d8-b273-821de3eac1d3","topic":"关键路径上的活动","expanded":true}]}]},{"id":"992a7195-635b-4d4c-93f4-77e011ce6d4c","topic":"过程","expanded":true,"children":[{"id":"ddc7dc1e-6604-4a68-af58-4bce16a1f884","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-a380e5591c810b.png","width":508.09,"height":262.86},"children":[{"id":"c9fbf3a4-93a5-4703-86a2-e40ee335e5a1","topic":"求所有事件的最早发生时间ve()","expanded":true,"children":[{"id":"84e5a59d-2cd2-4449-aec7-0475f3e024d1","topic":"V1  V2  V3  V4  V5  V6\nve(k)    0     3     2     6     6     8","expanded":true}]},{"id":"b5c0eb49-f09b-455f-a27d-8d65715167bd","topic":"求所有事件的最迟发生时间vl()","expanded":true,"children":[{"id":"e1fd5d4a-a412-4beb-84a0-038b7d76f20f","topic":"vl(k)    0     4     2     6     7     8","expanded":true}]},{"id":"3c2ac5a2-6e5c-44dd-957b-6e3cd09c01cf","topic":"求所有活动的最早发生时间e()","expanded":true,"children":[{"id":"4b045c21-01a0-4afa-9d70-f2e0ded24487","topic":"a1  a2   a3   a4   a5   a6   a7   a8\nve(k)    0     0     3     3     2     2     6     6","expanded":true}]},{"id":"d06186a1-aea8-4007-9f41-eaea996fe101","topic":"求所有活动的最迟发生时间l()","expanded":true,"children":[{"id":"299985b1-3f40-4345-a9e5-74f477036613","topic":"ve(k)    1     0     4     4     2     5     6     7","expanded":true}]},{"id":"b4a3975d-4808-464b-b5a8-f6a6bf871d43","topic":"求所有活动的时间余量d()","expanded":true,"children":[{"id":"2b1d53d0-8465-42c6-8380-66d174492a6e","topic":"ve(k)    1     0     1     1     0     3     0     1","expanded":true}]},{"id":"2cbce382-6fd2-4541-ab5f-0ca149af644b","topic":"关键活动（d()=0的活动就是关键活动）：a2、a5、a7\n关键路径：V1--->V3--->V4--->V6","expanded":true}]}]},{"id":"e45157f0-516d-49ea-ba1b-4ab4bf61ef2a","topic":"注意","expanded":true,"children":[{"id":"2ea05f44-c41d-4a15-8a0c-d719dfe4bdb2","topic":"关键路径上的所有活动都是关键活动，是决定整个工程的关键因素，因此可通过加快关键活动来缩短整个工程的工期","expanded":true},{"id":"44ac80cd-a829-4849-b08c-e1c5b7ddaa71","topic":"不能任意缩短关键活动，因为一旦缩短到一定程度，该关键活动可能变成非关键活动","expanded":true},{"id":"8ebf1090-60b7-484d-a55d-d0456bcb1216","topic":"网中的关键路径不唯一，只有加快那些包含在所有关键路径上的关键活动才能达到缩短工期的目的","expanded":true},{"id":"c1fb7793-03c3-4c6c-9c8c-6e7dad7de9a6","topic":"若关键活动耗时增加，则整个工程的工期增长","expanded":true}]}]}]}]},"floatingNodes":[{"id":"b6c1e420-0bc1-49ba-ac57-c24a886b3a4f","topic":"代码","expanded":true,"children":[{"id":"36c2f6b7-7bbf-4a18-86f7-c815e6016844","topic":"图的存储结构","expanded":true,"children":[{"id":"6c6539d6-6147-45da-941f-b5845b71b997","topic":"邻接矩阵存储","expanded":true,"children":[{"id":"3905e9c1-b526-4d85-be5f-e9f88511db5d","topic":"g[a][b]存储边a->b","expanded":true}]},{"id":"1ef04144-c376-4014-b0a1-17b1f6bed45b","topic":"邻接表存储（数组模拟）","expanded":true,"children":[{"id":"b3072cba-b59e-43d6-ac4a-5430cddaada2","topic":"// 对于每个点k，开一个单链表，存储k所有可以走到的点。h[k]存储这个单链表的头结点\nint h[N], e[N], ne[N], idx;\n// 添加一条边a->b\nvoid add(int a, int b)\n{\n    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;\n}\n// 初始化\nidx = 0;\nmemset(h, -1, sizeof h); //在头文件cstring中","expanded":true}]}]},{"id":"eb40a582-5a7b-49aa-a147-c656603d2090","topic":"图的遍历","expanded":true,"style":{"branchColor":"#0288D1"},"children":[{"id":"b664533d-534a-43a4-9859-45ddc2f416fe","topic":"DFS","expanded":true,"children":[{"id":"3a435f03-3ab5-4245-aba9-f26217ca4c3e","topic":"上面的代码思路可以，针对不同题目代码差异大，不列举了","expanded":true}]},{"id":"5d776fda-a8ac-471c-bad7-ab436470a33d","topic":"BFS","expanded":true,"children":[{"id":"635c6bd3-9154-41d2-af7f-6a053327eb08","topic":"queue<int> q;  //STL中的队列容器\nst[1] = true; // 表示1号点已经被遍历过\nq.push(1);\n\nwhile (q.size())\n{\n    int t = q.front();\n    q.pop();\n    for (int i = h[t]; i != -1; i = ne[i])\n    {\n        int j = e[i];\n        if (!st[j])\n        {\n            st[j] = true; // 表示点j已经被遍历过\n            q.push(j);\n        }\n    }\n}","expanded":true}]}]},{"id":"f6fec24b-3e0b-45ca-8d3a-69cf037a56c7","topic":"最短路径问题导图","expanded":true,"children":[{"id":"99d6960c-c7d3-41b8-80fa-66bd501529da","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-fcd937cdc0cdda.png","width":679.67,"height":300.03},"children":[{"id":"d6236c74-9692-40cf-98a8-76211deabda0","topic":"朴素Dijkstra（适合稠密图，无负权值）","expanded":true,"children":[{"id":"459df0bd-8bd7-4075-944e-990d0dd6e02e","topic":"思路","expanded":true,"children":[{"id":"2f5d53b8-5720-448e-af62-76d3e7b5dac1","topic":"1. 初始化（初始化dist，邻接矩阵）\n2. 循环n-1次，每次找最短dist[t]，用t更新其它点的距离","expanded":true}]},{"id":"fd74f93f-f4fb-459b-a784-de7052763e1b","topic":"//初始化，b[]标记是否已经访问，dist[]表示最短路径，path[]表示前驱结点\nmemset(g,0x3f,sizeof g);\nmemset(dist,0x3f,sizeof dist);\ng[a][b]=min(c,g[a][b]); //如果有重边，需要选小的\n//Dijkstra\nbool dijkstra(){\n    dist[1]=0;\n    for(int i=0;i<n-1;i++){\n        int t=-1;\n        //找最小的dist\n        for(int j=1;j<=n;j++){\n            if((t==-1||dist[j]<dist[t])&&!b[j]){\n                t=j;\n            }\n        }\n        //更新已访问的点\n        b[t]=1;\n        //更新该点其它距离\n        for(int j=1;j<=n;j++)\n            dist[j]=min(g[t][j]+dist[t],dist[j]);\n    }\n    if(dist[n]==0x3f3f3f3f) return 0;\n    else return 1;\n}","expanded":true}]},{"id":"84c917ba-6fdf-4ea9-bfcf-886b916ba6a3","topic":"堆优化的Dijkstra（适合稀疏图，无负权值）","expanded":true,"children":[{"id":"81a51236-62df-4038-bb4c-fd893dfa5a02","topic":"思路","expanded":true,"children":[{"id":"8cc5e2c5-95af-4c5a-a07f-15b75f484442","topic":"在朴素Dijkstra算法的基础上\n初始化（初始化dist，邻接矩阵）\n循环n-1次，每次找最短dist[t]，用t更新其它点的距离\n其中找最短dist[t]需要O(n2)，故利用一个堆把该步骤降为O(n)\n随之用堆更新的时间复杂度上升为O(mlogn)，故适合于稀疏图","expanded":true}]},{"id":"837d6a92-4e9c-4a2f-8980-c29e2955babc","topic":"int dijkstra(){\n\tpriority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;\n    dist[1]=0;\n    q.push({0,1});\n    while(q.size()){\n        pair<int,int> temp=q.top();\n        q.pop();\n        if(st[temp.second]) continue; //若已访问直接跳过，若在下方if语句中判断时间差很多\n        st[temp.second]=1;\n        //更新操作\n        for(int i=h[temp.second];i!=-1;i=ne[i]){\n            int j=e[i];\n            if(dist[j]>temp.first+w[i]){\n                dist[j]=temp.first+w[i];\n                q.push({dist[j],j});\n            }\n        }\n    }\n    if(dist[n]!=0x3f3f3f3f) return dist[n];\n    else return -1;\n}","expanded":true}]},{"id":"9ed2a1c5-7949-4aee-9f51-3650aa92ca43","topic":"bellman-ford（负权值，若规定k条边最短路径则只能用它，O(nm)）","expanded":true,"children":[{"id":"809fae27-f90a-420f-81e3-4bbcbe4803d3","topic":"思路","expanded":true,"children":[{"id":"9b2093c9-08cf-46b5-989f-384875a9add7","topic":"思路：(很暴力，边的存储仅需要用结构体数组即可)\nfor n 次\n    用back数组备份dist，防止串联（访问前几条边改变数据后影响后面的访问，会破坏k条边的条件）\n    for 所有边 a，b，w\n         dist[b] = min(dist[b], back[a]+w); //松弛操作\n三角不等式：dist[b] <= dist[a]+w\n补充：如果执行n次依然更新了路径，说明有n条的最短路径，即有负权边","expanded":true}]},{"id":"15aeb142-41ff-4f29-955c-8fb4b0500d8a","topic":"struct line{ //定义结构体\n    int a,b,w;\n}lines[N];\n\nvoid bellman_ford(){\n    dist[1]=0;\n    for(int i=0;i<k;i++){\n        memcpy(back,dist,sizeof dist); //备份防止串联\n        for(int j=0;j<m;j++){\n            if(dist[lines[j].b]>back[lines[j].a]+lines[j].w){//这里用back\n                dist[lines[j].b]=back[lines[j].a]+lines[j].w;\n            }\n        }\n    }\n    if(dist[n]>0x3f3f3f3f/2) printf(\"impossible\");\n    else printf(\"%d\",dist[n]);\n}","expanded":true}]},{"id":"6215b317-84aa-4947-895b-52f6fb86523c","topic":"SPFA（bellman-ford优化，一般O(m),最坏O(nm)）","expanded":true,"children":[{"id":"2a5bfb88-c013-4a82-b9c8-a4f26f32c62d","topic":"思路","expanded":true,"children":[{"id":"31d5462c-3e85-4ef2-8d2c-1580b5a8f119","topic":"在bellman-ford的基础上，利用一个队列与广度优先搜索，仅将变小的点加入队列中；","expanded":true}]},{"id":"615b29a4-0cc2-416a-b523-c20be0102991","topic":"补充","expanded":true,"children":[{"id":"c205c9a9-69fd-41df-88a5-ded3c1c9dbf3","topic":"用一个cnt[]数组维护每个节点的最短路径边数，若边数=n即有负权边。需要考虑图不连通，故需要初始化时把所有点加入队列，且dist都相等即可。","expanded":true}]},{"id":"8e71a7e6-8439-45ea-b285-dbf790525a65","topic":"void spfa(){\n    q.push(1);\n    dist[1]=0;\n    st[1]=1;\n    while(q.size()){\n        int t=q.front();\n        q.pop();\n        st[t]=0; //SPFA与Dijkstra不同仅在于标记数组用于标记是否重复进去队列而非是否访问过\n        for(int i=h[t];i!=-1;i=ne[i]){\n            int j=e[i];\n            if(dist[j]>dist[t]+w[i]){\n                dist[j]=dist[t]+w[i];\n                if(!st[j]){ //改变后进行判断是否需要加入队列\n                    st[j]=1;\n                    q.push(j);\n                }\n            }\n        }\n    }\n    if(dist[n]==0x3f3f3f3f) printf(\"impossible\");\n    else printf(\"%d\",dist[n]);\n}","expanded":true}]},{"id":"bdbaf92c-2c40-4221-a0ea-c2851590e08c","topic":"Floyd","expanded":true,"children":[{"id":"946f1876-8395-418c-84be-bcffb28a016b","topic":"思路","expanded":true,"children":[{"id":"ca89ac13-1240-4fd9-9410-378d58fbf6d9","topic":"三重循环，用邻接矩阵存储\n外层循环每个要加入的点，双层循环两个要加入的节点，如果可加入其中且路径边短则更新即可。","expanded":true}]},{"id":"a796ff14-c520-4f22-b552-7f8e70f9a49c","topic":"初始化：\n    for (int i = 1; i <= n; i ++ )\n        for (int j = 1; j <= n; j ++ )\n            if (i == j) d[i][j] = 0;\n            else d[i][j] = INF;\n\n// 算法结束后，d[a][b]表示a到b的最短距离\nvoid floyd()\n{\n    for (int k = 1; k <= n; k ++ ) //每次要加入的点\n        for (int i = 1; i <= n; i ++ ) //加入点i，j中\n            for (int j = 1; j <= n; j ++ )\n                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n}","expanded":true}]}]}]},{"id":"5670773e-8f41-4b83-977d-3ca33eaae69d","topic":"最小生成树与二分图导图","expanded":true,"style":{"branchColor":"#0288D1"},"children":[{"id":"b5372d4b-5712-4a29-b3b4-9f765f5e9115","topic":"朴素Prim算法用于稠密图，优化版Prim和Kruskal算法侧重于稀疏图，由于堆优化代码长，故一般直接用Kruskal算法","expanded":true},{"id":"30c8818d-10f5-45fe-b54d-1a6b65a3a71f","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-4f45d54ad993ec.png","width":757.07,"height":272.85},"children":[{"id":"ae02cc0f-7aba-472d-9aaa-3100868e18af","topic":"Prim","expanded":true,"children":[{"id":"7e0edccd-223e-435d-b11d-fa5d142cc34f","topic":"思路","expanded":true,"children":[{"id":"b7dea8ab-4553-46e6-87be-6361cfe2539f","topic":"几乎和dijkstra一样，区别在标记数组记录的是进入生成树的节点，dist数组记录的是各节点到生成树的最短路径；","expanded":true}]},{"id":"4326107b-a1ba-4368-b5d7-67e6a2564b86","topic":"void prim(){\n    dist[1]=0;\n    for(int i=0;i<n;i++){\n        int t=-1;\n        for(int j=1;j<=n;j++){\n            if(!st[j]&&(t==-1||dist[t]>dist[j])){\n                t=j;\n            }\n        }\n        st[t]=1;\n        res+=dist[t];\n        for(int j=1;j<=n;j++){\n            if(dist[j]>g[t][j]){\n                dist[j]=g[t][j];\n            }\n        }\n    }\n}","expanded":true}]},{"id":"2310b581-6249-4be7-84b0-6580458ea7ea","topic":"Kruskal","expanded":true,"children":[{"id":"a834f8d0-35b9-4abf-a97b-232a3307bc24","topic":"思路","expanded":true,"children":[{"id":"253819e1-f09b-4127-856e-54383a67f1a8","topic":"用结构体存边，并排序，从最短的边找起，若两点不在一颗树则连接在一起（并查集思想），直到所有点都用最短的边连在一起","expanded":true}]},{"id":"33a16622-c0a9-43c0-87a4-a6d278b84976","topic":"sort(lines,lines+m); //边排序\nfor(int i=0;i<m;i++){ \n    int a=find(lines[i].a),b=find(lines[i].b);\n    if(a!=b){\n        h[b]=a; //若不是一棵树操作\n        res+=lines[i].c;\n        cnt++;\n    }\n}","expanded":true,"children":[{"id":"bc8157f4-4668-4755-968e-d86a619dab11","topic":"并查集中find函数","expanded":true}]}]}]}]}],"x":2465.2,"y":901.4}]}