Week3 Parshan Complete
Week3 Parshan Complete
1A, 1C
Week 3
Parshan Teimouri
• Problems
• Q&A
Slido link for questions
2
Graph Algorithms
• |V| = n
• # of vertices (or nodes)
• |E| = m
• # of edges
• m ∈ 𝑂(𝑛! )
• A node can have an edge to itself: self-loop
Slido link for questions
4
Graph
• Undirected Graphs
• Directed Graphs
• Also called Digraph
Image from Fionda, Valeria & Palopoli, Luigi. (2011). Biological Network Querying Techniques: Analysis and Comparison. Journal of computational biology : a journal of
computational molecular cell biology. 18. 595-625. 10.1089/cmb.2009.0144. 5
Graphs: Representation
1) Adjacency Lists
• 𝑂(𝑛 + 𝑚) space
• Degree of a node:
• Num of edges
6
Image from https://www.lavivienpost.com/implement-graph-as-adjacency-list/
Graphs: Representation
2) Adjacency Matrices
• 𝑂(𝑛! ) space
7
Graphs: Representation
2) Adjacency Matrices
• Directed graph version
8
Definitions
• Path
• When you move on the graph from u to v
• You can’t repeat edges
• Vertices can be repeated
• Simple path
• A path with no repeated vertices
9
Definitions
• A Cycle
• When you visit several nodes and come back to the
start node while moving on graph edges
• Simple Cycle
• A cycle that has distinct nodes
10
Definitions
• Connected graphs
• Intuitively, graph is not separated
11
Definitions
• Directed graphs
• Strongly connected
• For each u, v:
• Weakly connected
• If directions are dropped: The resulting graph is connected
Slido link for questions
12
Definitions
• Distance between two nodes
• Dist(u,v) = min length of all u->v paths
• For Digraphs:
• the length of a shortest directed path from u to v
13
Definitions
• Trees
• A connected Graph that has no cycle
• m = n-1
14
Definitions
• Trees
• A connected Graph that has no cycle (loop)
• Rooted trees
• Ancestor-descendant
• Forest
Slido link for questions
15
Definitions
• Leaf node
• In undirected graphs: If the node has degree one
16
Definitions
• Tree properties: Any two of the following statements implies the third
1. G is connected
2. G does not contain a cycle
3. G has n−1 edges
17
Graph Traversal
• Visualization: link
Slido link for questions
18
Graph Traversal
• Breadth-First Search (BFS)
• BFS Tree
Start here
19
Graph Traversal
• Implementing BFS
• Queue
20
Bipartite Graphs
• Definition: if we can partition the nodes into two sets (red/ blue) so that there are
no edges between nodes of the same set
21
Bipartite Graphs
• A graph is bipartite iff it has no cycles of odd length.
22
Bipartite Graphs
• A graph is bipartite iff it has no cycles of odd length
1. If a graph is bipartite: it doesn’t have cycles of odd length
• Proof by contradiction
23
Bipartite Graphs
• A graph is bipartite iff it has no cycles of odd length
2. If there is no cycles of odd length: the graph is bipartite
• Algorithm
• Pick a node and Run BFS
• Paint the layers of BFS with red/blue alternately
• We argue that this is a valid coloring, which proves the graph is bipartite
• BWOC: assume there is an edge between two nodes of the same layer
• Name the nodes u, v
• Get their lowest common ancestor (LCA): call it t
• Length of the cycle (u->v->t->u) is: 2L + 1
• Length is odd: Contradiction
• So: there is no edge between nodes of the same layer in our BFS tree
Slido link for questions
• Mutual reachability
• If for two given nodes u, v we have:
• A path from u to v
• A path from v to u
• u and v are mutually reachable
• Strong connectivity
• Each two nodes: mutually reachable
Slido link for questions
25
Connectivity in Directed Graphs
• A property of mutual reachability
1. If u, v are mutually reachable
2. And v, w are mutually reachable
• Then, u and w are mutually reachable too
• Prove for practice
26
Connectivity in Directed Graphs
• Given a digraph G and one of its nodes s: how to find all the paths that end in s?
27
Connectivity in Directed Graphs
• You have a node s. how to get the paths that end in s?
• Reverse the directionality of all edges: Grev
28
Connectivity in Directed Graphs
• Checking if a directional graph is strongly connected
• if s has a path to and from all the nodes: the graph is strongly connected
Slido link for questions
29
Directed Acyclic Graph (DAG)
• An undirected graph without cycle: a forest (several disconnected trees) or a tree
• Example:
• i<j: draw an edge from i to j
• How many edges does it have?
Bergsten, Ulla and Johan Schubert. “Dempster's rule for evidence ordered in a complete directed acyclic graph.” ArXiv cs.AI/0305014 (1993): n. pag. 30
Topological Ordering
• Topological ordering (sort):
• An ordering of all nodes: 𝑣" , …, 𝑣#
31
Directed Acyclic Graph (DAG)
• Problem:
• Prove a topological ordering exists for a DAG
32
Directed Acyclic Graph (DAG)
• Problem
• Prove a topological ordering exists for a DAG
• Solution
• Fact: the first node in topological ordering must have no incoming edges
33
Directed Acyclic Graph (DAG)
• Solution
• Fact: the first node in topological ordering must have no incoming edges
• Proof: by contradiction
• Imagine there is no node with incoming edge = 0
• Then, for all nodes would have at least one incoming edge
34
Directed Acyclic Graph (DAG)
• Solution
• Claim: There exists a node in the DAG with no incoming edges
• Proof: by contradiction
• Imagine there is no node with incoming edge = 0
• Then, for all nodes would have at least one incoming edge
• these steps results in a contradiction
• Pick any arbitrary node
• Go to one of its parents
• You can always find a parent because they have at least one incoming edge
• There will be a node we saw twice after n steps Slido link for questions
• Using induction on the number of nodes: there is a topological ordering for a DAG of
n nodes
• Base case: n=1
Slido link for questions
• Now, using the correctness of this argument for n=k, we prove it for n=k+1 36
Directed Acyclic Graph (DAG)
• Using induction on the number of nodes: there is a topological ordering for a DAG of
n nodes
• Base case: n=1
• Now, using the correctness of this argument for n=k, we prove it for n=k+1
• Find the node with in-degree=0: call it v
• Just proved its existence
37
Directed Acyclic Graph (DAG)
• Using induction on the number of nodes: there is a topological ordering for a DAG of n nodes
• Base case: n=1
• Now, using the correctness of this argument for n=k, we prove it for n=k+1
• Induction gives us the topological ordering for G-{v} Slido link for questions
• O(n) for removing this node from the graph (removing its edges)
39
Directed Acyclic Graph (DAG)
• Better algorithm:
• Definition: Active nodes: those that are not yet deleted
• Keep track of:
1. For all nodes: save the # of incoming edges from active nodes
2. A list of all active nodes with no incoming edges
• Review
• 𝑉(𝐺) is the set of all vertices of the graph 𝐺.
• Number of vertices: 𝑉(𝐺) = 𝑛
• 𝐸(𝐺) is the set of all edges in the graph 𝐺.
• Number of edges: 𝐸(𝐺) = 𝑚
• Adjacency Matrix (shown by A(𝐺)) is a 𝑛×𝑛 matrix that shows existence of edges between nodes.
• A[i, j] = 1 if there is an edge from vi to vj, else 0
• A(𝐺) is symmetric for undirected graphs. May be or not be symmertric for digraphs
• Incidence Matrix (shown by M(𝐺)) is a 𝑛×𝑚 matrix that shows which nodes are endpoints of an edge
• M[i, j] = 1 if vi is an endpoint of ej, else 0 Slido link for questions
42
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
43
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
44
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
• Review
• Simple Graph: There isn’t more than one edge between any two vertices, and there is no self-loop
• Disconnected graph: at least two nodes u, v exist for which there is no path between them
̅ Made by dropping existing edges in 𝐺 and drawing non existing edges
• Complement of a Graph (𝐺):
• Hint
• To decide about connectivity of the mentioned graph, browse the paths in 𝐺̅ and 𝐺. Slido link for questions
45
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
46
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
47
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
• Review
• Bipartite Graph: A graph in which nodes can be divided to two disjoint sets U and V, where
• Each edge connects a node in U to a node in V
• Hints
• The format of the problem is “X if and only if Y.” To prove this statement, you must prove
• X implies Y (Necessity)
• Y implies X (Sufficiency)
Slido link for questions
• Try to related the parity (even or odd) of the length of existing paths to formation of the partite sets
48
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
49
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
50
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
• Hints
• The format of the problem is “X if and only if Y.” To prove this statement, you must prove
• X implies Y (Necessity)
• Y implies X (Sufficiency): to prove connectivity given Y, you can use proof by contrapositive, or use an
algorithmic approach.
• By contrapositive: To prove 𝑃 → 𝑄, we prove ¬𝑄 → ¬𝑃
• Algorithmic approach: Showing how the paths are formed between each two nodes
51
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
52
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
53
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
• A vertex v is an articulation point (also called cut vertex) if removing v increases the number of
connected components. Suggest an algorithm for finding all the articulation points in a connected
graph.
54
Source: geeksforgeeks.org
Problems
• A vertex v is an articulation point (also called cut vertex) if removing v increases the number of
connected components. Suggest an algorithm for finding all the articulation points in a connected
graph.
• Hints
• Run a DFS on the graph, and find the characteristics of articulation points on the DFS tree.
• Remember that on a DFS tree there is no edge between children of a node (Prove for practice) and their subtrees
55
Source: geeksforgeeks.org
Problems
• A vertex v is an articulation point (also called cut vertex) if removing v increases the number of connected
components. Suggest an algorithm for finding all the articulation points in a connected graph.
56
Source: geeksforgeeks.org
Problems
• A vertex v is an articulation point (also called cut vertex) if removing v increases the number of
connected components. Suggest an algorithm for finding all the articulation points in a connected
graph.
• Solution
• In DFS tree, a vertex u is an articulation point if one of the following two conditions is true.
• u is the root of the DFS tree and it has at least two children.
• u is not the root of the DFS tree and it has a child v such that no vertex in the subtree rooted with v has a back
edge to one of the ancestors in DFS tree of u.
Slido link for questions
57
Source: geeksforgeeks.org
Problems
• Hints
• = you cannot swap entries of a permutation odd number of times and arrive at itself
• find a variable that is even at both initial and final steps, but changes in odd amount at each swap.
• Count inversions: The number of indexes i, j, for which i<j but ai>aj Slido link for questions
58
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
59
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
60
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
61
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
62
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
63
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
64
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
65
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
66
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
67
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems
68
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Q&A