0% found this document useful (0 votes)
38 views

Week3 Parshan Complete

Uploaded by

Ab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Week3 Parshan Complete

Uploaded by

Ab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

CS180 Discussion

1A, 1C
Week 3

Parshan Teimouri

Slido link for questions


Outline
• Review
• Graph Algorithms
• Definitions
• Graph Traversal
• Bipartite Graphs
• Connectivity in Directed Graphs
• Topological ordering

• Problems

• Q&A
Slido link for questions

2
Graph Algorithms

Slido link for questions


Graph
• G = (V, E)

• |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

Slido link for questions

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

• Sum of all degrees?


• ∑$!"# deg(𝑣! )=?
Slido link for questions

6
Image from https://www.lavivienpost.com/implement-graph-as-adjacency-list/
Graphs: Representation
2) Adjacency Matrices
• 𝑂(𝑛! ) space

• For undirected graphs: symmetric

Slido link for questions

7
Graphs: Representation
2) Adjacency Matrices
• Directed graph version

Slido link for questions

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

• Simple path on digraphs?


• Similar to paths on undirected graphs
• Except that, must follow the directionality
Slido link for questions

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

Slido link for questions

10
Definitions
• Connected graphs
• Intuitively, graph is not separated

• More formally: There is a path between each two node

Slido link for questions

11
Definitions
• Directed graphs

• Strongly connected
• For each u, v:

• Both u->v and v->u paths exist

• 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

Slido link for questions

13
Definitions
• Trees
• A connected Graph that has no cycle

• Has the min number of edges to be connected

• m = n-1

Slido link for questions

14
Definitions
• Trees
• A connected Graph that has no cycle (loop)

• Rooted trees

• Ancestor-descendant

• Leaf: nodes with degree=1


• The root node can be a leaf node as well

• Forest
Slido link for questions

15
Definitions
• Leaf node
• In undirected graphs: If the node has degree one

• In digraphs: out-degree of the node is zero

Slido link for questions

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

Slido link for questions

17
Graph Traversal

• Breadth-First Search (BFS)


• Adding nodes one layer at a time
• = discover all neighbors of a node before moving on
to the next node

• Layer number: the distance of the nodes from the


root
• BFS tree: the result of running BFS on a graph

• Visualization: link
Slido link for questions

18
Graph Traversal
• Breadth-First Search (BFS)
• BFS Tree
Start here

Construction of BFS tree


a) Exploring node 1 and discovering 2, 3
b) Exploring node 2, 3
c) Exploring nodes in layer 3

A sample Graph Slido link for questions

19
Graph Traversal
• Implementing BFS
• Queue

• First in First out

Slido link for questions

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

Slido link for questions

21
Bipartite Graphs
• A graph is bipartite iff it has no cycles of odd length.

• Iff=you can prove either side given the other


1. If a graph is bipartite: it doesn’t have any cycle of odd length

2. If there is no cycle of odd length: the graph is bipartite

Slido link for questions

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

• Get the cycle: 1, 2, 3, …, 2n-1, 1

• Color them: red, blue, red, blue


• you will run into a contradiction for the color of last node

Slido link for questions

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

• A valid 2-coloring of nodes is proposed. Hence, the graph is bipartite


24
Connectivity in Directed Graphs
• Representing Directed Graphs
• For each node, create two lists
• One for outgoing edges, one for incoming edges

• 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

• Strong components: Maximal strong subgraphs of a graph


• Strong: each two nodes in the subgraph are mutually reachable
• Maximal: the size of the set cannot be increased
• If any node is added to this set of vertices, the result won’t be strong anymore
• Notice the difference between maximum and maximal Slido link for questions

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?

Slido link for questions

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

• run BFS/DFS from s

• Each path from s in Grev is a path to s in G

Slido link for questions

28
Connectivity in Directed Graphs
• Checking if a directional graph is strongly connected

• Graph G and a node s:


• Run BFS from s
• Checks if there is a path from s to all other nodes

• Reverse the directionality of all edges


• Run BFS from S again
• Checks if there is a path other nodes to s

• 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

• DAGs: Can have many edges (as opposed to trees)

• Example:
• i<j: draw an edge from i to j
• How many edges does it have?

Slido link for questions

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: 𝑣" , …, 𝑣#

• If i<j on the order: there cannot be an edge from 𝑣$ to 𝑣%


• = no edges in the opposite direction of order

Slido link for questions

31
Directed Acyclic Graph (DAG)
• Problem:
• Prove a topological ordering exists for a DAG

• Suggest an algorithm find a topological order

Slido link for questions

32
Directed Acyclic Graph (DAG)
• Problem
• Prove a topological ordering exists for a DAG

• Suggest an algorithm find a topological order

• Solution
• Fact: the first node in topological ordering must have no incoming edges

Slido link for questions

33
Directed Acyclic Graph (DAG)
• Solution
• Fact: the first node in topological ordering must have no incoming edges

• 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

Slido link for questions

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

• Take the parent of the current node


• repeat n times

• There will be a node we saw twice after n steps Slido link for questions

• We have found a cycle: Contradiction: a DAG cannot have a cycle 35


Directed Acyclic Graph (DAG)
• Back to the problem
• Prove a topological ordering exists for a DAG

• Suggest an algorithm find a topological order

• 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

• G-{v} has no cycles


• Why? Slido link for questions

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

• Find the node with in-degree=0: call it v


• Just proved its existence
• G-{v} has no cycles

• Induction gives us the topological ordering for G-{v} Slido link for questions

• Answer for n=k+1 is: v + topological_order(G-{v}) 38


Directed Acyclic Graph (DAG)
• Time complexity of this algorithm?

• O(n^2): for each iteration, we have:


• O(n) for finding a node with incoming_edges=0

• O(n) for removing this node from the graph (removing its edges)

• N iterations of O(n): O(n^2)

Slido link for questions

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

• Initializing: single pass over all data: O(n+m)


• In each iteration
• Select a node from 2 and delete it
• Go over its outgoing edges and update 1
• If incoming edges of any node dropped to zero after the update: add that node to 1
Slido link for questions

• Time complexity improved: O(n+m)


40
Problems

Slido link for questions


Problems

• 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

Slido link for questions

43
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

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

Slido link for questions

46
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

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

• No two nodes in the same set are adjacent

• In a Bipartite Graph, there is no cycle of odd length

• 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

Slido link for questions

49
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

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

Slido link for questions

51
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

52
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

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.

Slido link for questions

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

Slido link for questions

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.

Slido link for questions

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

• We want to prove the graph has no odd cycle

• = 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

Slido link for questions

59
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

60
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

61
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

62
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

63
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

64
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

65
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

66
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

67
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Problems

Slido link for questions

68
West, Douglas Brent. Introduction to graph theory. Vol. 2. Upper Saddle River: Prentice hall, 2001.
Q&A

Slido link for questions

You might also like