CC Chapter 4 Tree and Graph --Graph
来源:互联网 发布:机械臂编程 编辑:程序博客网 时间:2024/05/29 09:14
1. Graphs
A tree is actually a type of graph, but not all graphs are trees. Simply put, a tree is a connected graph without cycles.
A graph is simply a collection of nodes with edges between (some of) them.
- Graphs can be either directed or undirected. While directed edges are like a one-way street, undirected edges are like a two-way street.
- The graph might consist of multiple isolated subgraphs. If there is a path between every pair of vertices, it is called a connected graph.
- The graph can also have cycles(or not). An "Acyclic graph" is one without cycles.
Representation
(1) Adjacency List
Every vertex stores a list of adjacent vertices. In an undirected graph, an edge like (a, b) store twice. once in a's adjacent vertices and once in b's adjacent vertices.
class Graph{ public Node[] nodes;}class Node{ public String name; public Node[] children;}
(2) Adjacency Matrices
An adjacency matrix is an N* N boolean matrix( where N is the number of nodes), where a true value at matrix[i][j] indicates an edge from node i to node j. (or 0s and 1s)
2. Graph Search
DFS(depth-first search): start at root, explore each branch completely before moving onto the next branch. Go deep first we go wide.
BFS(Breadth -first search): start at root, and explore each neighbor before going on to any of their children. Go wide before we go deep.
void DFS(Node root){ if(root == null) return; visit(root); root.visited = true; foreach(Node n in root.adjacent){ if(n.visited == false){ DFS(n); } }}
void BFS(Node root){ Queue queue = new Queue(); root.marked = true; queue.enqueue(root); //add to the end of queue while(!queue.isEmpty()){ Node r = queue.dequeue(); //remove from the front of the queue visit(r); foreach(Node in r.adjacent){ if(n.marked == false){ n.marked = true; queue.enqueue(n); } } }}
- CC Chapter 4 Tree and Graph --Graph
- CC chapter 4 Tree and Graphs ----Tree
- Covisibility Graph and Essential Graph
- graph
- Graph
- Graph
- graph
- Graph
- Graph
- Graph
- graph
- Graph
- Graph
- graph
- Graph
- Graph
- graph
- Graph
- 字符串和数组的相互转换
- C# Ping的简单应用
- Python:字典
- JS概述
- (OK) NS3
- CC Chapter 4 Tree and Graph --Graph
- 2.7 CSS background属性
- 冒泡排序
- 快速排序
- Redis的安装和部署
- URAL
- CSS入门笔记
- 2.8 CSS相对定位&&绝对定位&&固定定位&&z-index
- 1060. 爱丁顿数