COMP 2711H: Lecture 15
Date: 2024-10-07 09:06:10
Reviewed:
Topic / Chapter:
summary
βQuestions
Notes
Graph Theory
-
Tree
- theorem: when edge is added to a tree , creates exactly 1 cycle
- proof
- there will be at least one cycle: as it has edges now
- there can't be two cycle: as doing so would mean following situation
- and, in this case, input contained cycle itself
graph TD 1((1)) 2((2)) 3((3)) 4((4)) 1-->2 2-->3 1-->4 1--e-->3 4-->3
- a weighted graph consists of
- : a graph
- finding: shortest path
- where distance of each edge:
- walk is more accurate, as it might not be a path
- previous theorem: every connected graph contains a subgraph
- s.t. is a tree
- such tree: spanning tree
- theorem: given a weighted graph , a subtree
- is a minimum spanning tree (MST) if it is a spanning tree w/ least total weight
- algorithm to find MST: Kruskal's algorithm
- sort: each weight in increasing order
- for each (from minimum): take the edge if it doesn't create a cycle
- π¨βπ« what is the cheapest edge to connect two connected components?
- there might be multiple MST!!
- above algorithm: finds one such MST
- π¨βπ« what is the maximum number of MST?
- algorithm works: as
- it terminates as there are finite no. of edges
- output: connected graph given input is connected
- proof: suppose the output is not connected
- i.e. there exists
- and : vertices in
- algorithm: went to all possible edges, should have checked
- let : output
- and be MST
- show:
- : sum of weights in graph (tree)
- i.e. algorithm: chooses the same edge with
- and first edge to be not in :
- consider:
- it is a cycle
- however, the output is a tree
- thus: must contain a path to connect vertices of
- let it be
- claim: is a tree
- but the method of choosing : edge w/ smallest possible weight
- thus, must have a smaller weight than
- contradiction!
- then: repeat until
- sometimes, algorithm: much simpler than proof of correctness!
- Prim's algorithm
- picks a vertex
- then pick a cheapest edge from
- let it be to
- then, pick a cheapest edge from , then repeat
- condition: not making a cycle!
- π¨βπ« what is the cheapest edge to connect an edge to my current cc?
- proof: similar to Kruskal's
- π¨βπ« hint: assume ideal path, and compare & contrast
- modify it until
-
Directed graph
- directed graph:
- where : set of vertices
- and of fork
- π¨βπ« it's not a set, thus order matters
- directed equivalent of connected component
- candidate 1:
- ignore the direction
- consider it cc if its undirected case is a cc
- π¨βπ« not very useful, as one cannot move one in cycle to another
- candidate 1:
- vertices : strongly connected (SC) iff
- both -path and -path exists
- strongly connected component (SCC): maximal subset of vertices
- s.t. every two of them are SC
- strongly connected graph: partitions your graph
- every component: SC to itself
- if SC to -> vice versa
- and ->
- transitive
- DAG: directed acyclic graph
- example
- directed graph:
graph TD 1((1)) 2((2)) 3((3)) 1-->2 2-->3 3-->1
- : has no cycle iff: every vertex of : its own SCC
- assume: has no cycles
- let : be two vertices
- if in the same SCC: there must be both and path
- then: connection of and path: closed walk
- and all closed walk: implies existence of cycle
- π¨βπ« proof: exactly same fore directed graph
- contradiction, as is assumes to have no cycle
- thus: every vertex of : its own SCC
- without extra members
- let : be two vertices
- assume: every vertex of : its own SCC
- let : be a cycle
- if : then both and path exist
- thus, they must be in SCC
- due to assumption: each cycle: contains at most 1 vertex
- loop: meaningless
- assume: has no cycles
-
Topological ordering
- topological ordering: given directed graph
- permutation of vertices s.t. every edge : of the form
- theorem: directed graph : a DAG iff has a topological ordering
- proof
- : w/ topological ordering: implies there is no cycle:
- i.e. is a DAG
- no cycle implies topological ordering
- if there is no cycle: must exist a vertex w/ indegree 0
- unless: has a cycle
- assume: we create a left-to-right indegree
- π¨βπ« similar to prerequisite problem, ain't it
- if there is no cycle: must exist a vertex w/ indegree 0
- d
- : w/ topological ordering: implies there is no cycle:
- for directed graph, both indegree and outdegree are defined
- lemma: if a directed , if outdegree of every vertex: at least 1
- then there is a cycle
- proof: keep going until you find a previously seen vertex
- must end somewhere as the graph is finite
- similar lemma: if a directed , if indegree of every vertex: at least 1
- then there is a cycle
- proof: keep going backwards until you find a previously seen vertex
- must end somewhere as the graph is finite
- topological ordering: given directed graph
-
DAG game
- DAG game: a game w/ loopless DAG
- two players w/ starting vertex
- w/ shared piece / position
- each turn: player chooses an outgoing edge
- player who can't make a move: loses
- two players w/ starting vertex
- example board
- DAG game: a game w/ loopless DAG
graph LR 1(( )) 2(( )) 3(( )) 4(( )) 5(( )) 6(( )) 7(( )) 8(( )) 9(( )) 10(( )) 11((L)) 1-->2 2-->3 1-->4 4-->3 1-->5 5-->3 3-->6 3-->7 7-->8 8-->9 8-->10 9-->11 10-->11
- play: a path on a DAG
- assume: both players are smart / doesn't make mistake
- = optimal play
- properties
- game: always ends
- because: there is no cycle
- and thus, there are at most (tree-like) steps
- because: there is no cycle
- every vertex: either or (win / lose)
- generally: if it has edge to vertex: it is a
- if it only has edge to vertex: it is a
- generally: if it has edge to vertex: it is a
- game: always ends
- successor: vertices that a vertex has (directed)
-
Rooted trees
- rooted tree: tree , in which : chosen as root
- s.t. it is ordered: from root to outwards
- π¨βπ« sometimes it's the other way
- tree: can be thought of a descendants, etc.
- π¨βπ« interestingly, we often locate tree at the top
- vertex without a successor / child: leaf
- π¨βπ« definition of leaf: varies