mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update README.md
This commit is contained in:
parent
cc05ef2ea8
commit
93a6452f57
112
graphs/README.md
112
graphs/README.md
@ -4,16 +4,22 @@
|
||||
|
||||
* graph is a non-linear data structure consisting of vertices and edges.
|
||||
|
||||
|
||||
* graphs can be represented by adjacent matrices, adjacent lists, and hash table of hash tables.
|
||||
|
||||
|
||||
* in **undirected graphs**, the edges between any two vertices do not have a direction, indicating a two-way relationship.
|
||||
|
||||
|
||||
* in **directed graphs**, the edges between any two vertices are directional.
|
||||
|
||||
|
||||
* in **weighted graphs**, each edge has an associated weight. if the sum of the weights of all edges of a cycle is a negative values, it's a negative weight cycle.
|
||||
|
||||
|
||||
* the **degree of a vertex** is the number of edges connecting the vertex. in directed, graphs, if the **in-dregree** of a vertex is `d`, there are **d** directional edges incident to the vertex (and similarly, **out-degree** from the vertex).
|
||||
|
||||
|
||||
* with `|V|` the number of vertices and `|E|` is the number of edges, search in a graph (either bfs of dfs) is `O(|V| + |E|)`.
|
||||
|
||||
<br>
|
||||
@ -62,6 +68,60 @@ def bfs(matrix):
|
||||
|
||||
<br>
|
||||
|
||||
* or as a class:
|
||||
|
||||
<br>
|
||||
|
||||
```python
|
||||
|
||||
from collections import deque
|
||||
|
||||
|
||||
class Graph:
|
||||
|
||||
def __init__(self, edges, n):
|
||||
|
||||
self.adj_list = [[] for _ in range(n)]
|
||||
|
||||
for (src, dest) in edges:
|
||||
self.adj_list[src].append(dest)
|
||||
self.adj_list[dest].append(src)
|
||||
|
||||
|
||||
def bfs(graph, v, discovered):
|
||||
|
||||
queue = deque(v)
|
||||
discovered[v] = True
|
||||
|
||||
while queue:
|
||||
|
||||
v = queue.popleft()
|
||||
print(v, end=' ')
|
||||
|
||||
for u in graph.adj_list[v]:
|
||||
if not discovered[u]:
|
||||
discovered[u] = True
|
||||
queue.append(u)
|
||||
|
||||
|
||||
def recursive_bfs(graph, queue, discovered):
|
||||
|
||||
if not queue:
|
||||
return
|
||||
|
||||
v = queue.popleft()
|
||||
print(v, end=' ')
|
||||
|
||||
for u in graph.adj_list[v]:
|
||||
if not discovered[u]:
|
||||
discovered[u] = True
|
||||
queue.append(u)
|
||||
|
||||
recursive_bfs(graph, queue, discovered)
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
----
|
||||
|
||||
#### depth first search
|
||||
@ -96,3 +156,55 @@ def dfs(matrix):
|
||||
traverse(i, j)
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
* or as a class:
|
||||
|
||||
<br>
|
||||
|
||||
```python
|
||||
from collections import deque
|
||||
|
||||
class Graph:
|
||||
|
||||
def __init__(self, edges, n):
|
||||
|
||||
self.adj_list = [[] for _ in range(n)]
|
||||
|
||||
for (src, dest) in edges:
|
||||
self.adj_list[src].append(dest)
|
||||
self.adj_list[dest].append(src)
|
||||
|
||||
|
||||
def dfs(graph, v, discovered):
|
||||
|
||||
discovered[v] = True
|
||||
print(v, end=' ')
|
||||
|
||||
for u in graph.adj_list[v]:
|
||||
if not discovered[u]: #
|
||||
dfs(graph, u, discovered)
|
||||
|
||||
|
||||
|
||||
def iterative_dfs(graph, v, discovered):
|
||||
|
||||
stack = [v]
|
||||
|
||||
while stack:
|
||||
|
||||
v = stack.pop()
|
||||
|
||||
if discovered[v]:
|
||||
continue
|
||||
|
||||
discovered[v] = True
|
||||
print(v, end=' ')
|
||||
|
||||
adj_list = graph.adjList[v]
|
||||
for i in reversed(range(len(adj_list))):
|
||||
u = adj_list[i]
|
||||
if not discovered[u]:
|
||||
stack.append(u)
|
||||
```
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user