Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
מבוא לתורת הגרפים
0
2
1
3
2
Graph representation
• A graph is a set of vertices or nodes connected by edges
• A graph may be directed or undirected
0
2
1
3
3
Graph representation
• We will represent graphs using a 2-dimentional list:
0
2
1
3
(0) (1) (2) (3)
(0)
(1)
(2)
(3)
4
Graph representation
• We will represent graphs using a 2-dimentional list:
0
2
1
3
(0) (1) (2) (3)
(0) 0 2 2 1
(1) 2 0 0 1
(2) 2 0 0 1
(3) 1 1 1 0
>>> g = [[0,2,2,1], [2,0,0,1], [2,0,0,1], [1,1,1,0]]
5
Undirected graph
• Can this matrix represent an undirected graph?
>>> g = [[0,1,0], [1,1,1], [0,0,0]]
6
Undirected graph
• Can this matrix represent an undirected graph?
• No! an undirected graph must be represented
by a symmetric matrix:• For every entry: g[i][j] = g[j][i]
>>> g = [[0,1,0], [1,1,1], [0,0,0]]
7
exercise
• Assume in a graph g, for every entry: g[i][j] = g[j][i].
Does it necessarily mean that g is undirected?
8
exercise
• Write a function called is_symmetric
• Argument:• graph – a 2-dimentional list representing a graph
• Returns True if the graph is (possibly) undirected, i.e.,For every entry: g[i][j] = g[j][i]
9
exercise
• Write a function called is_symmetric
• Argument:• graph – a 2-dimentional list representing a graph
• Returns True if the graph is (possibly) undirected, i.e.,For every entry: g[i][j] = g[j][i]
def is_symmetric(g):
n = len(g) #number of nodes in the graph
for i in range(n):
for j in range(n):
if g[i][j] != g[j][i]: #not symmetric!
return False
return True
10
Bipartite graph
• Definition: an undirected bipartite graph is a graph with the following properties:
• Its vertices can be divided into two disjoint sets 𝑈, 𝑉
so that every edge connects a vertex in 𝑈 to one in 𝑉
• No edges between two vertices in U
• No edges between two vertices in V
11
cliques
• Definition: a clique is a graph where every two vertices are connected by an edge
12
cliques
• Definition: a clique is a graph where every two vertices are connected by an edge
13
exercise
• Question: a graph is a clique the graph is bipartite?
14
exercise
• Question: a graph is a clique the graph is bipartite?
• NO
• Proof: Consider a clique of 3 vertices G
15
exercise
• Question: a graph is a clique the graph is bipartite?
• NO
• Proof: Consider a clique of 3 vertices G
• Divide G into 2 disjoint sets U,V
UV
16
exercise
• Question: a graph is a clique the graph is bipartite?
• NO
• Proof: Consider a clique of 3 vertices G
• Divide G into 2 disjoint sets U,V
• Observe: exactly one subset (either U or V)has at least two vertices, call two of these vertices 𝑣1, 𝑣2
UV
17
exercise
• Question: a graph is a clique the graph is bipartite?
• NO
• Proof: Consider a clique of 3 vertices G
• Divide G into 2 disjoint sets U,V
• Observe: exactly one subset (either U or V)has at least two vertices, call two of these vertices 𝑣1, 𝑣2
• As all vertices are connected, so are 𝑣1, 𝑣2!
UV
18
exercise
• Question: a graph is a clique the graph is bipartite?
• NO
• Proof: Consider a clique of 3 vertices G
• Divide G into 2 disjoint sets U,V
• Observe: exactly one subset (either U or V)has at least two vertices, call two of these vertices 𝑣1, 𝑣2
• As all vertices are connected, so are 𝑣1, 𝑣2!
• Alternately, G contains an odd length cycle
UV
19
exercise
• In a house full of ginger and grey cats• Every ginger cat screams if it sees another ginger cat.
• Every grey cat screams if it sees another grey cat
• Can we spread the cats around the house so that no 2 cats of the same color stay in adjacent rooms, and avoid the screaming?
20
exercise
• In a house full of ginger and grey cats• Every ginger cat screams if it sees another ginger cat.
• Every grey cat screams if it sees another grey cat
• Can we spread the cats around the house so that no 2 cats of the same color stay in adjacent rooms, and avoid the screaming?
• We will build a graph where every room is a vertex.
• Every 2 vertices will be connected with an edge if the 2 rooms are adjacent.
• Check if we can divide the graph into 2 disjoint sets of ‘rooms’, for gingers and for greys.
21
exercise
• Prove: A graph is bipartite ⇔ its vertices can be colored in two colors such that there is no edge connecting two vertices of the same color
תנאי מספיק והכרחי לכך שניתן לצבוע את קודקודי -היות הגרף דו צדדי: כלומר•מאותו הצבעקודקודיםהגרף בשני צבעים כך ששום קשת לא מחברת
22
exercise
• Prove: A graph is bipartite its vertices can be colored in two colors such that there is no edge connecting two vertices of the same color
23
exercise
• Prove: A graph is bipartite its vertices can be colored in two colors such that there is no edge connecting two vertices of the same color
• Proof:
• The graph is bipartite, lets divide its vertices to disjoint
Sets.
24
exercise
• Prove: A graph is bipartite its vertices can be colored in two colors such that there is no edge connecting two vertices of the same color
• Proof:
• The graph is bipartite, lets divide its vertices to disjoint
Sets.
• We will color each set with different color
25
exercise
• Prove: A graph is bipartite its vertices can be colored in two colors such that there is no edge connecting two vertices of the same color
26
exercise
• Prove: A graph is bipartite its vertices can be colored in two colors such that there is no edge connecting two vertices of the same color
• Proof:
• A graph can be colored with red and blue such that
There is no edge connecting blue vertex to a red one
27
exercise
• Prove: A graph is bipartite its vertices can be colored in two colors such that there is no edge connecting two vertices of the same color
• Proof:
• A graph can be colored with red and blue such that
There is no edge connecting blue vertex to a red one
• Lets divide the vertices into 2 sets:• R= all the red vertices
• B = all the blue vertices
28
exercise
• Prove: A graph is bipartite its vertices can be colored in two colors such that there is no edge connecting two vertices of the same color
• Proof:
• A graph can be colored with red and blue such that
There is no edge connecting blue vertex to a red one
• Lets divide the vertices into 2 sets:• R= all the red vertices• B = all the blue vertices
• no edge connects red vertex to a blue one
• The graph is bipartite with R,B partition.
29
אוילרמעגל
:מהשיעור•
.בכל קשת בדיוק פעם אחתבגרף הוא מסלול שעובר אוילרמסלול •
אוילרמעגל זהו , אם מסלול כזה מסתיים באותו צומת בו הוא התחיל•
:אוילרמשפט •
.כל הצמתים בעלי דרגה זוגית: אוילרמעגלתנאי מספיק והכרחי לקיום •
30
אוילרמסלול
:המשך-אוילרמשפט •
בעלי 2כל הצמתים למעט בדיוק : אוילרמסלולתנאי מספיק והכרחי לקיום •.דרגה זוגית
31
אוילרמסלול
:המשך-אוילרמשפט •
בעלי 2כל הצמתים למעט בדיוק : אוילרמסלולתנאי מספיק והכרחי לקיום •.דרגה זוגית
:אינטואיציה•
-אוילרגרף עם מעגל נקח•
.דרגת כל הצמתים היא זוגית•
32
אוילרמסלול
:המשך-אוילרמשפט •
בעלי 2כל הצמתים למעט בדיוק : אוילרמסלולתנאי מספיק והכרחי לקיום •.דרגה זוגית
:אינטואיציה•
-אוילרגרף עם מעגל נקח•
.דרגת כל הצמתים היא זוגית•נוכל להתחיל את , אוילרכיוון וזה מעגל •
המסלול בכל קשת שנרצה
33
אוילרמסלול
:המשך-אוילרמשפט •
בעלי 2כל הצמתים למעט בדיוק : אוילרמסלולתנאי מספיק והכרחי לקיום •.דרגה זוגית
:אינטואיציה•
-אוילרגרף עם מעגל נקח•
.דרגת כל הצמתים היא זוגית•נוכל להתחיל את , אוילרכיוון וזה מעגל •
המסלול בכל קשת שנרצה
נמחק את הקשת האחרונה במעגל•
34
אוילרמסלול
:המשך-אוילרמשפט •
בעלי דרגה 2כל הצמתים למעט בדיוק : אוילרמסלולתנאי מספיק והכרחי לקיום •.זוגית
:אינטואיציה•
-אוילרגרף עם מעגל נקח•
.דרגת כל הצמתים היא זוגית•נוכל להתחיל את , אוילרכיוון וזה מעגל •
המסלול בכל קשת שנרצה
.נמחק את הקשת האחרונה במעגל•
שחוברו על ידי הקשת הזוהקודקודים•
.כעת בעלי דרגה אי זוגית, (ורק הם)
35
אוילרמעגל \מסלול
• Lets write a function euler
• Argument:• A graph
• Prints ‘Euler cycle’ if the graph contains an Euler cycle
• Prints ‘Euler path’ if the graph contains an Euler path
• Prints ‘No Euler path’ otherwise.
• Assume G is undirected, connected and has no self edges
36
אוילרמעגל \מסלול
• Lets write a function euler
• Argument:• A graph
• Prints ‘Euler cycle’ if the graph contains an Euler cycle
• Prints ‘Euler path’ if the graph contains an Euler path
• Prints ‘No Euler path’ otherwise.
• Assume G is undirected, connected and has no self edges
• Solution: count how many vertices have an odd degree.
37
אוילרמעגל \מסלול
def euler (G):
n = len(G)
odd = 0
for i in range(n):
deg = sum(G[i])
if deg%2 == 1:
odd+=1
if odd == 0:
print(“Euler cycle”)
elif odd == 2:
print(“Euler path”)
else:
print(“No Euler path”)38
אוילרמעגל \מסלול
def euler (G):
n = len(G)
odd = 0
for i in range(n):
deg = sum(G[i])
if deg%2 == 1:
odd+=1
if odd == 0:
print(“Euler cycle”)
elif odd == 2:
print(“Euler path”)
else:
print(“No Euler path”)
Number of nodes in G
39
אוילרמעגל \מסלול
def euler (G):
n = len(G)
odd = 0
for i in range(n):
deg = sum(G[i])
if deg%2 == 1:
odd+=1
if odd == 0:
print(“Euler cycle”)
elif odd == 2:
print(“Euler path”)
else:
print(“No Euler path”)
Number of nodes in G
Number of edges that ‘touch’ node i
40
אוילרמעגל \מסלול
• Can you draw the ‘House of Santa’ without raising your pencil?
41
אוילרמעגל \מסלול
• Can you draw the ‘House of Santa’ without raising your pencil?
• Or: does the ‘House of Santa’ graph have an Euler path?
42