[DS:10] Graph 1

  • Definition
    • Edge : some relation
      • Undirected Graphs G1
        • Facebook
        • (u,v)
      • Directed Graphs
        • Twitter
        • 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)
    • Neighbor
      • out neighbor:right
      • in neighbor:left
    • Degree
      • the number of neighbors of this
        • if directed
          • out-degree
          • in-degree
    • 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
    • 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
  • 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
  • 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
  • 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
      • 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即可。
        • 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?
      • 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
    • 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 是顶点的出度。因为你需要遍历该顶点的所有出边,所以时间复杂度与该顶点的出度相同。
        • } Get a vertex’s in-edges:
          • O(|E|) (could keep a second adjacency list for this!)
            • 你需要遍历图中的所有边来找到所有的入边。但是,如果你保留了一个反向的邻接列表,那么你可以在 O(d) 的时间内完成这个操作,d 是顶点的入度。
        • } Decide if some edge exists:
          • O(d) where dis out-degree of source
            • d 是源顶点的出度。因为你需要在源顶点的出边中搜索这条边。
        • } Insert an edge:
          • O(1) (unless you need to check if it’s already there)
            • 通常情况下,你可以在 O(1) 的时间内在源顶点的邻接列表中添加这条边。但是,如果你需要检查这条边是否已经存在,那么时间复杂度将会是 O(d),d 是源顶点的出度。
        • } Delete an edge:
          • O(d) where dis out-degree of source
            • d 是源顶点的出度。你需要在源顶点的邻接列表中找到并删除这条边。
      • Adjacency lists also work well for undirected graphs with one caveat
        • } Put each edge in two lists to support efficient "get all neighbors"

    • 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
  • 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)
  • 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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值