fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@@ -0,0 +1,125 @@
---
title: Breadth First Search (BFS)
localeTitle: 广度优先搜索BFS
---
## 广度优先搜索BFS
广度优先搜索是最简单的图算法之一。它首先检查当前节点,然后通过将其后继添加到下一级别来扩展它,从而遍历图形。在移动到下一级别之前,对当前级别中的所有节点重复该过程。如果找到解决方案,搜索将停止。
### 可视化
![](https://upload.wikimedia.org/wikipedia/commons/4/46/Animated_BFS.gif)
### 评估
空间复杂度On
更糟糕的案例时间复杂性On
广度优先搜索在有限的节点集上完成,并且如果从一个节点移动到另一个节点的成本是恒定的则是最优的。
### BFS实现的C ++代码
```cpp
// Program to print BFS traversal from a given
// source vertex. BFS(int s) traverses vertices
// reachable from s.
#include<iostream>
#include <list>
using namespace std;
// This class represents a directed graph using
// adjacency list representation
class Graph
{
int V; // No. of vertices
// Pointer to an array containing adjacency
// lists
list<int> *adj;
public:
Graph(int V); // Constructor
// function to add an edge to graph
void addEdge(int v, int w);
// prints BFS traversal from a given source s
void BFS(int s);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v's list.
}
void Graph::BFS(int s)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;
// Create a queue for BFS
list<int> queue;
// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push_back(s);
// 'i' will be used to get all adjacent
// vertices of a vertex
list<int>::iterator i;
while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();
// Get all adjacent vertices of the dequeued
// vertex s. If a adjacent has not been visited,
// then mark it visited and enqueue it
for (i = adj[s].begin(); i != adj[s].end(); ++i)
{
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
}
// Driver program to test methods of graph class
int main()
{
// Create a graph given in the above diagram
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Following is Breadth First Traversal "
<< "(starting from vertex 2) \n";
g.BFS(2);
return 0;
}
```
#### 更多信息:
[图表](https://github.com/freecodecamp/guides/computer-science/data-structures/graphs/index.md)
[深度优先搜索DFS](https://github.com/freecodecamp/guides/tree/master/src/pages/algorithms/graph-algorithms/depth-first-search/index.md)

View File

@@ -0,0 +1,105 @@
---
title: Depth First Search (DFS)
localeTitle: 深度优先搜索DFS
---
## 深度优先搜索DFS
深度优先搜索是最简单的图算法之一。它通过首先检查当前节点然后移动到其中一个成员来重复该过程来遍历图形。如果当前节点没有要检查的进程,我们将返回其前身并继续进程(通过移动到另一个进程)。如果找到解决方案,搜索将停止。
### 可视化
![](https://upload.wikimedia.org/wikipedia/commons/7/7f/Depth-First-Search.gif)
### 实现C ++ 14
\`\`\`C ++
# 包括
# 包括
# 包括
# 包括
使用命名空间std;
class Graph { int v; //顶点数
```
// pointer to a vector containing adjacency lists
vector < int > *adj;
```
上市: 图int v; //构造函数
```
// function to add an edge to graph
void add_edge(int v, int w);
// prints dfs traversal from a given source `s`
void dfs();
void dfs_util(int s, vector < bool> &visited);
```
};
Graph :: Graphint v{ 这 - > v = v; adj = new vector <int> \[v\]; }
void Graph :: add _edgeint uint v{ adj \[u\] .push_ backv; //将v添加到你的列表中 adj \[v\] .push _backv; //将你添加到v的列表中如果图表被定向则删除此语句 } void Graph :: dfs{ //访问过的向量 - 跟踪DFS期间访问的节点 vector <bool> visitvfalse; //将所有节点/顶点标记为未访问 forint i = 0; i <v; i ++ 如果(!参观\[1\] dfs_ utilivisited; } //注意这里使用call-by-reference的用法 void Graph :: dfs\_utilint svector <bool>visited{ //将当前节点/顶点标记为已访问 访问过\[s\] =真; //将其输出到标准输出(屏幕) cout << s <<“”;
```
// traverse its adjacency list and recursively call dfs_util for all of its neighbours!
// (only if the neighbour has not been visited yet!)
for(vector < int > :: iterator itr = adj[s].begin(); itr != adj[s].end(); itr++)
if(!visited[*itr])
dfs_util(*itr, visited);
```
}
int main { //使用我们上面定义的Graph类创建图形 图g4; g.add _edge0,1; g.add_ edge0,2; g.add _edge1,2; g.add_ edge2,0; g.add _edge2,3; g.add_ edge3,3;
```
cout << "Following is the Depth First Traversal of the provided graph"
<< "(starting from vertex 0): ";
g.dfs();
// output would be: 0 1 2 3
return 0;
```
}
```
### Evaluation
Space Complexity: O(n)
Worse Case Time Complexity: O(n)
Depth First Search is complete on a finite set of nodes. I works better on shallow trees.
### Implementation of DFS in C++
```
C ++
# 包括
# 包括
# 包括
使用命名空间std;
struct Graph { int v; bool \* _adj; 上市: Graphint vcount; void addEdgeint uint v; void deleteEdgeint uint v; 向量_ _DFSint s; void DFSUtilint svector_ _DFS矢量_ _访问; }; Graph :: Graphint vcount{ this-> v = vcount; this-> adj = new bool_ \[vcount\]; forint i = 0; i
void Graph :: addEdgeint uint w{ - >形容词\[U\] \[W\] = TRUE; 这 - >形容词\[W\] \[U\] = TRUE; }
void Graph :: deleteEdgeint uint w{ 这 - >形容词\[U\] \[W\] = FALSE; 这 - >形容词\[W\] \[U\] = FALSE; }
void Graph :: DFSUtilint svector dfs矢量 &访问){ 访问了\[S\] = TRUE; dfs.push\_back一个或多个; forint i = 0; i V;我++{ ifthis-> adj \[s\] \[i\] == true && visited \[i\] == false DFSUtilIDFS访问; } }
向量 Graph :: DFSint s{ 向量访问(这 - > v的; 向量 DFS; DFSUtilSDFS访问; return dfs; } \`\`\`
#### 更多信息:
[图表](https://github.com/freecodecamp/guides/computer-science/data-structures/graphs/index.md)
[广度优先搜索BFS](https://github.com/freecodecamp/guides/tree/master/src/pages/algorithms/graph-algorithms/breadth-first-search/index.md)
[深度优先搜索DFS - 维基百科](https://en.wikipedia.org/wiki/Depth-first_search)

View File

@@ -0,0 +1,95 @@
---
title: Dijkstra's Algorithm
localeTitle: Dijkstra的算法
---
# Dijkstra的算法
Dijkstra算法是由EW Dijkstra提出的图算法。它在具有非负边的图中找到单源最短路径。为什么
我们创建了2个数组visit和distance它们分别记录是否访问了顶点以及距离源顶点的最小距离。最初访问的数组被指定为false距离指定为无限。
我们从源顶点开始。设当前顶点为u其相邻顶点为v。现在对于与u相邻的每个v如果之前未访问过该距离并且距离u的距离小于其当前距离则更新距离。然后我们选择距离最小且未访问过的下一个顶点。
优先级队列通常用于在最短的时间内满足最后的要求。下面是使用Java中的优先级队列实现相同的想法。
```java
import java.util.*;
public class Dijkstra {
class Graph {
LinkedList<Pair<Integer>> adj[];
int n; // Number of vertices.
Graph(int n) {
this.n = n;
adj = new LinkedList[n];
for(int i = 0;i<n;i++) adj[i] = new LinkedList<>();
}
// add a directed edge between vertices a and b with cost as weight
public void addEdgeDirected(int a, int b, int cost) {
adj[a].add(new Pair(b, cost));
}
public void addEdgeUndirected(int a, int b, int cost) {
addEdgeDirected(a, b, cost);
addEdgeDirected(b, a, cost);
}
}
class Pair<E> {
E first;
E second;
Pair(E f, E s) {
first = f;
second = s;
}
}
// Comparator to sort Pairs in Priority Queue
class PairComparator implements Comparator<Pair<Integer>> {
public int compare(Pair<Integer> a, Pair<Integer> b) {
return a.second - b.second;
}
}
// Calculates shortest path to each vertex from source and returns the distance
public int[] dijkstra(Graph g, int src) {
int distance[] = new int[gn]; // shortest distance of each vertex from src
boolean visited[] = new boolean[gn]; // vertex is visited or not
Arrays.fill(distance, Integer.MAX_VALUE);
Arrays.fill(visited, false);
PriorityQueue<Pair<Integer>> pq = new PriorityQueue<>(100, new PairComparator());
pq.add(new Pair<Integer>(src, 0));
distance[src] = 0;
while(!pq.isEmpty()) {
Pair<Integer> x = pq.remove(); // Extract vertex with shortest distance from src
int u = x.first;
visited[u] = true;
Iterator<Pair<Integer>> iter = g.adj[u].listIterator();
// Iterate over neighbours of u and update their distances
while(iter.hasNext()) {
Pair<Integer> y = iter.next();
int v = y.first;
int weight = y.second;
// Check if vertex v is not visited
// If new path through u offers less cost then update distance array and add to pq
if(!visited[v] && distance[u]+weight<distance[v]) {
distance[v] = distance[u]+weight;
pq.add(new Pair(v, distance[v]));
}
}
}
return distance;
}
public static void main(String args[]) {
Dijkstra d = new Dijkstra();
Dijkstra.Graph g = d.new Graph(4);
g.addEdgeUndirected(0, 1, 2);
g.addEdgeUndirected(1, 2, 1);
g.addEdgeUndirected(0, 3, 6);
g.addEdgeUndirected(2, 3, 1);
g.addEdgeUndirected(1, 3, 3);
int dist[] = d.dijkstra(g, 0);
System.out.println(Arrays.toString(dist));
}
}
```

View File

@@ -0,0 +1,48 @@
---
title: Floyd Warshall Algorithm
localeTitle: Floyd Warshall算法
---
## Floyd Warshall算法
Floyd Warshall算法是一种很好的算法用于查找图中所有顶点之间的最短距离。它具有非常简洁的算法和OV ^ 3时间复杂度其中V是顶点数。它可以与负权重一起使用但图表中不得出现负权重循环。
### 评估
空间复杂度OV ^ 2
更糟糕的案例时间复杂性OV ^ 3
### Python实现
```python
# A large value as infinity
inf = 1e10
def floyd_warshall(weights):
V = len(weights)
distance_matrix = weights
for k in range(V):
next_distance_matrix = [list(row) for row in distance_matrix] # make a copy of distance matrix
for i in range(V):
for j in range(V):
# Choose if the k vertex can work as a path with shorter distance
next_distance_matrix[i][j] = min(distance_matrix[i][j], distance_matrix[i][k] + distance_matrix[k][j])
distance_matrix = next_distance_matrix # update
return distance_matrix
# A graph represented as Adjacency matrix
graph = [
[0, inf, inf, -3],
[inf, 0, inf, 8],
[inf, 4, 0, -2],
[5, inf, 3, 0]
]
print(floyd_warshall(graph))
```
#### 更多信息:
[图表](https://github.com/freecodecamp/guides/computer-science/data-structures/graphs/index.md)
[Floyd Warshall - 维基百科](https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm)

View File

@@ -0,0 +1,23 @@
---
title: Graph algorithms
localeTitle: 图算法
---
## 图算法
图算法是遍历访问a图的一组指令。
一些算法用于查找特定节点或两个给定节点之间的路径。
### 为什么图算法很重要
图形是非常有用的数据结构,可以模拟各种问题。这些算法直接应用于社交网站,状态机建模等等。
### 一些常见的图算法
一些最常见的图算法是:
[图表](https://github.com/freecodecamp/guides/computer-science/data-structures/graphs/index.md)
[广度优先搜索BFS](https://github.com/freecodecamp/guides/tree/master/src/pages/algorithms/graph-algorithms/breadth-first-search/index.md)
[深度优先搜索DFS](https://github.com/freecodecamp/guides/tree/master/src/pages/algorithms/graph-algorithms/depth-first-search/index.md)