42
מבוא כללי לתכנות ולמדעי המחשב תרגול מס' 8 דין שמואל[email protected] 1

8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

מבוא כללי לתכנות ולמדעי המחשב

8'תרגול מס

דין שמואל[email protected]

1

Page 2: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

מבוא לתורת הגרפים

0

2

1

3

2

Page 3: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 4: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

Graph representation

• We will represent graphs using a 2-dimentional list:

0

2

1

3

(0) (1) (2) (3)

(0)

(1)

(2)

(3)

4

Page 5: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 6: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

Undirected graph

• Can this matrix represent an undirected graph?

>>> g = [[0,1,0], [1,1,1], [0,0,0]]

6

Page 7: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 8: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

exercise

• Assume in a graph g, for every entry: g[i][j] = g[j][i].

Does it necessarily mean that g is undirected?

8

Page 9: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 10: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 11: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 12: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

cliques

• Definition: a clique is a graph where every two vertices are connected by an edge

12

Page 13: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

cliques

• Definition: a clique is a graph where every two vertices are connected by an edge

13

Page 14: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

exercise

• Question: a graph is a clique the graph is bipartite?

14

Page 15: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

exercise

• Question: a graph is a clique the graph is bipartite?

• NO

• Proof: Consider a clique of 3 vertices G

15

Page 16: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 17: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 18: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 19: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 20: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 21: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 22: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 23: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 24: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 25: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 26: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 27: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 28: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 29: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

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

Page 30: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמעגל

:מהשיעור•

.בכל קשת בדיוק פעם אחתבגרף הוא מסלול שעובר אוילרמסלול •

אוילרמעגל זהו , אם מסלול כזה מסתיים באותו צומת בו הוא התחיל•

:אוילרמשפט •

.כל הצמתים בעלי דרגה זוגית: אוילרמעגלתנאי מספיק והכרחי לקיום •

30

Page 31: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמסלול

:המשך-אוילרמשפט •

בעלי 2כל הצמתים למעט בדיוק : אוילרמסלולתנאי מספיק והכרחי לקיום •.דרגה זוגית

31

Page 32: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמסלול

:המשך-אוילרמשפט •

בעלי 2כל הצמתים למעט בדיוק : אוילרמסלולתנאי מספיק והכרחי לקיום •.דרגה זוגית

:אינטואיציה•

-אוילרגרף עם מעגל נקח•

.דרגת כל הצמתים היא זוגית•

32

Page 33: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמסלול

:המשך-אוילרמשפט •

בעלי 2כל הצמתים למעט בדיוק : אוילרמסלולתנאי מספיק והכרחי לקיום •.דרגה זוגית

:אינטואיציה•

-אוילרגרף עם מעגל נקח•

.דרגת כל הצמתים היא זוגית•נוכל להתחיל את , אוילרכיוון וזה מעגל •

המסלול בכל קשת שנרצה

33

Page 34: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמסלול

:המשך-אוילרמשפט •

בעלי 2כל הצמתים למעט בדיוק : אוילרמסלולתנאי מספיק והכרחי לקיום •.דרגה זוגית

:אינטואיציה•

-אוילרגרף עם מעגל נקח•

.דרגת כל הצמתים היא זוגית•נוכל להתחיל את , אוילרכיוון וזה מעגל •

המסלול בכל קשת שנרצה

נמחק את הקשת האחרונה במעגל•

34

Page 35: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמסלול

:המשך-אוילרמשפט •

בעלי דרגה 2כל הצמתים למעט בדיוק : אוילרמסלולתנאי מספיק והכרחי לקיום •.זוגית

:אינטואיציה•

-אוילרגרף עם מעגל נקח•

.דרגת כל הצמתים היא זוגית•נוכל להתחיל את , אוילרכיוון וזה מעגל •

המסלול בכל קשת שנרצה

.נמחק את הקשת האחרונה במעגל•

שחוברו על ידי הקשת הזוהקודקודים•

.כעת בעלי דרגה אי זוגית, (ורק הם)

35

Page 36: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמעגל \מסלול

• 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

Page 37: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמעגל \מסלול

• 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

Page 38: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמעגל \מסלול

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

Page 39: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמעגל \מסלול

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

Page 40: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמעגל \מסלול

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

Page 41: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמעגל \מסלול

• Can you draw the ‘House of Santa’ without raising your pencil?

41

Page 42: 8 'סמ לוגרתcs4cyber.wdfiles.com/local--files/recitations/rec8_final.pdf · Graph representation •A graph is a set of vertices or nodes connected by edges •A graph may be

אוילרמעגל \מסלול

• Can you draw the ‘House of Santa’ without raising your pencil?

• Or: does the ‘House of Santa’ graph have an Euler path?

42