- Definition
- Edge : some relation
- Undirected Graphs G1
- (u,v)
- Directed Graphs
- Each edge will be related
- (u,v) means u is a source and v is the destination
- (u,v) != (v,u)
- Multi-edge(less seen in reality)
- Self-edge (u,u)
- Undirected Graphs G1
- Neighbor
- out neighbor:right
- in neighbor:left
- Degree
- the number of neighbors of this
- if directed
- out-degree
- in-degree
- if directed
- the number of neighbors of this
- Connected
- If there’s a path from every vertex to another
- A graph does not have to be connected
- even if every node has non-zero degree
- A graph can have multiple connected components
- Undirected graph Connectivity
- An undirected graph is connected if for all pairs of vertices u≠v, there exists a path from u to v
- An undirected graph is complete, or fully connected, if for all pairs of vertices u≠v there exists an edgefrom u to v
- Directed graph Connectivity
- Three notations
- A directed graph is strongly connected if there is a path from every vertex to every other vertex
- A directed graph is weakly connected if there is a path from every vertex to every other vertex ignoring direction of edges
- A direct graph is complete or fully connected, if for all pairs of vertices u ≠v , there exists an edgefrom u to v
- Three notations
- More notations
- G=(V,E):
- |V| is the number of vertices
- |E| is the number of edges
- Minimum: 0
- Max for undirected: |V| * (|V|-1) / 2 (不允许自环)|V| * (|V| + 1) / 2 (允许自环)
- Max for directed: |V| * (|V|-1)(不允许自环)|V| * |V|(允许自环)
- Attention: Here |V| does not mean absolute value but the number of elements
- G=(V,E):
- Edge : some relation
- Words and translations
- “Orthogonal” 是用来表示两个概念或事物是独立的,没有直接关系,翻译为正交。
- Weighted Graphs
- each edge has a weight or cost
- Typically,numeric
- Orthogonal to whether graph is connected
- Some allow negative weight, many don’t.
- Paths and cycles
- if there is a list of vertices such that (v1, v2)belongs to E for all I in the range
- A cycle is a path that begins and end at the same
- Path length and costs
- Path length : num of edges in the path (called unweighted cost for some scenarios)
- Path cost: sum of each cost
- Simple path and Cycles
- repeats no vertices in the path(except the first might be the last)
- Simple cycle is a cycle with simple path
- Tree
- A tree should be a graph
- undirected
- acyclic (no cycles)
- connected
- Rooted trees
- identify a unique root(anyone with different hierarchy relationships
- We “think” of edges are directed:parent to children
- How to choose the root to make it as balanced as possible
- A tree should be a graph
- DAGs Directed Acyclic Graphs
- A DAG is a directed graph without directed cycles
- But not every DAG is a rooted directed tree
- A Directed graph ✅
- But not every directed graph is a DAG
- A DAG is a directed graph without directed cycles
- Representation
- Adjacency Matrix 邻接矩阵
- Assign each node a number from 0 to |V|-1
- A |V| x |V| matrix of Booleans (or 0 vs. 1)
- Then M[u][v] == true means there is an edge
- A |V| x |V| matrix of Booleans (or 0 vs. 1)
- Properties
- Running time to:
- } Get a vertex’s out-edges:O(|V|)
- 因为你需要遍历顶点的整行来找到所有的出边。
- } Get a vertex’s in-edges:O(|V|)
- 因为你需要遍历顶点的整列来找到所有的入边。
- } Decide if some edge exists:O(1)
- 你可以直接通过查找矩阵的两个索引来确定边是否存在。
- } Insert an edge:O(1)
- 你只需要将矩阵的相应位置改为1(或者如果是带权图,改为相应的权值)即可。
- } Delete an edge:O(1)
- 你只需要将矩阵的相应位置改为0即可。
- } Get a vertex’s out-edges:O(|V|)
- Space requirements: O(V square)
- Best for sparse or dense graphs? dense
- How will the adjacency matrix vary for an undirected graph?
- } Will be symmetric about diagonal axis
- } Matrix: Could we save space by using only about half the array?
- Running time to:
- How can we adapt the representation for weighted graphs?
- } Instead of Boolean, store a number in each cell
- } Need some value to represent ‘not an edge’
- ◦ 0, -1, or some other value based on how you are using the graph
- ◦ Might need to be a separate field if no restrictions on weights
- Assign each node a number from 0 to |V|-1
- Adjacency List 邻接列表
- Assign each node a number from 0 to |V|-1
- } An array of length |V| in which each entry stores a list of all adjacent vertices (e.g., linked list)
- Running time to:
- } Get a vertex’s out-edges:
- O(d) where d is out-degree of vertex
- d 是顶点的出度。因为你需要遍历该顶点的所有出边,所以时间复杂度与该顶点的出度相同。
- O(d) where d is out-degree of vertex
- } Get a vertex’s in-edges:
- O(|E|) (could keep a second adjacency list for this!)
- 你需要遍历图中的所有边来找到所有的入边。但是,如果你保留了一个反向的邻接列表,那么你可以在 O(d) 的时间内完成这个操作,d 是顶点的入度。
- O(|E|) (could keep a second adjacency list for this!)
- } Decide if some edge exists:
- O(d) where dis out-degree of source
- d 是源顶点的出度。因为你需要在源顶点的出边中搜索这条边。
- O(d) where dis out-degree of source
- } Insert an edge:
- O(1) (unless you need to check if it’s already there)
- 通常情况下,你可以在 O(1) 的时间内在源顶点的邻接列表中添加这条边。但是,如果你需要检查这条边是否已经存在,那么时间复杂度将会是 O(d),d 是源顶点的出度。
- O(1) (unless you need to check if it’s already there)
- } Delete an edge:
- O(d) where dis out-degree of source
- d 是源顶点的出度。你需要在源顶点的邻接列表中找到并删除这条边。
- O(d) where dis out-degree of source
- } Get a vertex’s out-edges:
- Adjacency lists also work well for undirected graphs with one caveat
- } Put each edge in two lists to support efficient "get all neighbors"
- Assign each node a number from 0 to |V|-1
- Difference
- Graphs are often sparse
- } Streets form grids
- } Airlines rarely fly to all cities
- } Slower performance compensated by greater space savings
- Adjacency lists should generally be your default choice
- Different trade-offs, particularly time versus space
- Adjacency Matrix 邻接矩阵
- BFS (Breadth-First-Search)
- Intuition of BFS
- ◦ Given a source node s, always visit nodes that are closer to the source s first before visiting the others
- The result is not unique, if we do not define an order among out-going edges from a node
- If we impose an order by going from smaller id to larger id, then the result will be unique
- STEPS
- At the beginning, color all nodes to be white
- Create a queue Q, enqueue the source s to Q, and color the source to be gray (meaning s is in the queue)
- Repeat the following until queue � is empty
- ◦ Dequeue from Q, let the node be v
- ◦ For every out-neighbor u of v that is still white
- Enqueue u into Q, and color u to gray (to indicate u is in queue)
- ◦ Color v to be black (meaning v has finished)
- Time Complexity [DS:10.2-3]Introduction of BFS and DFS
- Intuition of BFS
- DFS (Depth-first search)
- Going along one path until we cannot go further
- Imposing an order to make the traversal unique: from smaller id to larger id
- We still focus on the directed graph
- Extension to undirected graph will be straightforward
- Initialization:
- ◦ At the beginning, color all nodes to be white
- ◦ Create a stack S, push the source s to S, and color the source to be gray (meaning s is in the stack)
- Repeat the following until S is empty
- ◦ Get the top node, denoted as v, on stack S, do not pop v
- ◦ If v still has white out-neighbors
- Let u be such a white out-neighbor of v
- Push u to S, and color u to gray
- ◦ Otherwise (v has no white out-neighbors)
- Pop v and color it as black (meaning that v has finished)
- Time Complexity [DS:10.2-3]Introduction of BFS and DFShttps://mubu.com/doc3Dr3hWgg4E0
- Properties if DFS [DS:10.2-3]Introduction of BFS and DFShttps://mubu.com/doc3Dr3hWgg4E0
- Going along one path until we cannot go further
[DS:10] Graph 1
最新推荐文章于 2024-05-19 21:34:53 发布