• Tidak ada hasil yang ditemukan

PDF Data structure and algorithm in Python - Graph

N/A
N/A
Protected

Academic year: 2023

Membagikan "PDF Data structure and algorithm in Python - Graph"

Copied!
82
0
0

Teks penuh

(1)

Data structure and algorithm in Python

Graph

Seongjin Lee

Dept of Aerospace and Software Engineering, Gyeongsang National University

(2)

Table of contents

1. Graphs

2. Data Structures for Graphs 3. Graph Traversals

(3)

Graphs

(4)

Graphs

Example : Graphs

A graph G is simply a set V of verticesand a collection E of pairs of vertices from V, called edges. A graph is a way of representing connections or relationships between pairs of objects from some set V.

(5)

Graphs

Edges in a graph are either directed or undirected.

• An edge (u,v)is said to bedirected fromutov if the pair (u,v)is ordered, withu precedingv.

• An edge (u,v)is said to beundirectedif the pair(u,v)is not ordered.

Undirected edges are sometimes denoted with set notation, as{u,v}, but for simplicity we use the pair notation(u,v), noting that in the

undirected case(u,v)is the same as(v,u).

3

(6)

Graphs

Definition : undirected, directed and mixed graph

• If all the edges in a graph are undirected, then we say the graph is an undirected graph.

• Likewise, a directed graph, also called a digraph, is a graph whose edges are all directed.

• A graph that has both directed and undirected edges is often called a mixed graph.

An undirected or mixed graph can be converted into a directed graph by replacing every undirected edge(u,v)by the pair of directed edges(u,v) and(v,u).

(7)

Graphs

• The two vertices joined by an edge are called theend vertices (or endpoints)of the edge.

• If an edge is directed, its first endpoint is its originand the other is thedestinationof the edge.

• Two verticesuandv are said to beadjacentif there is an edge whose end vertices areu andv.

5

(8)

Graphs

• Anedge issaidtobeincidenttoavertexifthevertexisoneofthe edge’s endpoints.

• The outgoing edges of a vertex are the directed edges whose origin is thatvertex.

• Theincomingedgesofavertexarethedirected edgeswhose destination is that vertex.

(9)

Graphs

• Thedegreeofavertexv,denoted deg(v),is thenumberof incidentedges ofv.

• The in-degree and out-degree of a vertex v are the number of the incomingandoutgoingedges ofv,andaredenotedindeg(v)and outdeg(v), respectively.

7

(10)

Graphs

• Thedefinitionofagraphreferstothegroup ofedgesasacollection, not a set, thus allowing two undirected edges to have the same end vertices,andfortwodirectededges tohave thesameoriginandthe same destination.Suchedges arecalledparalleledges ormultiple edges.

• Anotherspecialtypeofedge isonethatconnectsavertextoitself.

Namely, we say that an edge (undirected or directed) is a ring or a self-loopifitstwoendpointscoincide.

(11)

Graphs

• Withfew exceptions,graphsdonothaveparalleledgesorself-loops.

Such graphsaresaidtobesimple.

• Thus, we can usually say that the edges of a simple graph are a set ofvertexpairs(and notjustacollection).

Throughout this chapter, we assume that a graph is simple unless otherwise specified.

9

(12)

Graphs

• A path is a sequence of alternating vertices and edges that starts at a vertexandendsatavertexsuchthateachedge isincidenttoits predecessor andsuccessor vertex.

• A cycle is a path that starts and ends at the same vertex, and that includes atleastoneedge.

1. Apathis simpleifeachvertexinthepathisdistinct.

2. A cycle is simple if each vertex in the cycle is distinct, except for thefirstandlastone.

3. Adirected pathisapathsuchthatalledgesaredirectedandare traversed alongtheir direction.

4. Adirected cycleissimilarlydefined.

(13)

Graphs

• Adirected graphisacyclicifithasnodirected cycles.

• Ifagraphis simple,wemayomittheedgeswhendescribingpathP orcycleC, asthese arewelldefined,inwhichcasePisalist of adjacent vertices and C is a cycle of adjacent vertices.

11

(14)

Graphs

• Givenverticesuandv ofa(directed)graph G,wesaythatu reachesv,andthatv isreachablefromu,ifG hasa (directed)path from u to v.

• Inanundirectedgraph,thenotionofreachability issymmetric,that is to say, u reaches v if an only if v reaches u.

• However, in a directed graph, it is possible that u reaches v but v doesnotreachu,becauseadirectedpathmustbe traversed accordingtotherespectivedirectionsoftheedges.

(15)

• Agraph isconnectedif, foranytwovertices,thereisapath betweenthem.

• A directed graph ⃗G is strongly connected if for any two vertices u andvofG,ureachesvandvreachesu.

13

(16)

• Asubgraph ofagraphG is agraphH whoseverticesandedgesare subsets of the vertices and edges of G, respectively.

• Aspanningsubgraph ofG isasubgraph ofG thatcontainsallthe verticesofthegraphG.

• If a graph G is not connected, its maximal connected subgraphs are called theconnectedcomponentsofG.

• A forest is a graph without cycles.

• Atreeisa connectedforest,thatis,aconnectedgraphwithout cycles.

• A spanning tree of a graph is a spanning subgraph that is a tree.

(17)

Graphs

Proposition

IfG is a graph withmedges and vertex setV , then

v∈V

deg(v)=2m.

15

(18)

Graphs

Proposition

IfG is a directed graph withmedges and vertex set V , then

v∈V

indeg(v)=

v∈V

outdeg(v)=m.

(19)

Graphs

Proposition

Let G be a simple graph withnvertices andmedges.

• IfG is undirected, then

mn(n1) 2

• IfG is directed, then

mn(n1).

17

(20)

Graphs

Proposition

Let G be a undirected graph withnvertices andmedges.

• IfG is connected, then

mn1.

• IfG is a tree, then

m=n1.

• IfG is a forest, then

mn1.

(21)

Graphs

The Graph ADT

(22)

The Graph ADT

Since a graph is a collection of vertices and edges, we model the abstraction as a com- bination of three data types:

• Vertex

• Edge

• Graph

(23)

The Graph ADT

AVeretexis lightweight object that stores an arbitrary element provided by the user, supporting the method:

• element(): Retrieve the stored element.

20

(24)

The Graph ADT

AnEdgestores an associated object, supporting the following methods:

• element(): Retrieve the stored element.

• endpoints(): Return a tuple(u,v)such that vertexu is the origin of the edge and vertex v is the destination; for an undirected graph, the orientation is arbitrary.

• opposite(v): Assuming vertexv is one endpoint of the edge (either origin or destination), return the other endpoint.

(25)

The Graph ADT

The primary abstraction for a graph is theGraphADT. We presume that a graph can be either undirected or directed, with the designation declared upon construction; recall that a mixed graph can be represented as a directed graph, modeling edge{u,v}as a pair of directed edges(u,v) and(v,u).

22

(26)

The Graph ADT

The Graph ADT includes the following methods

• vertex_count(): Return the number of vertices.

• vertices(): Return aniterationof all vertices.

• edge_count(): Return the number of edges.

• edges(): Return aniterationof all edges.

• get_edge(u,v): Return the edge from vertexutov, if one exists;

otherwise return None.

• degree(v, out=True):

• For an undirected graph, return the number of edges incident to vertexv.

• For a directed graph, return the number of outgoing (resp. incoming) edges incident to vertexv, as designated by the optional parameter.

(27)

The Graph ADT

• incident_edges(v, out=True):

• Return an iteration of all edges incident to vertexv.

• In the case of a directed graph,

report outgoing edges by default;

report incoming edges if the optional parameter is set to False.

• insert_vertex(x=None): Create and return a new Vertex storing element x.

• insert_edge(u, v, x=None): Create and return a new Edge from vertexuto vertex v, storing elementx (None by default).

• remove_vertex(v): Remove vertexv and all its incident edges from the graph.

• remove_edge(e): Remove edge e from the graph.

24

(28)

Data Structures for Graphs

(29)

Data Structures for Graphs

• Edge List

• Adjacency List

• Adjacency Map

• Adjacency Matrix

25

(30)

Edge List

(31)

Adjacency List

27

(32)

Adjacency Map

(33)

Adjacency Matrix

29

(34)

Data Structures for Graphs

Python Implementation

(35)

Python Implementation

c l a s s G r a p h : c l a s s V e r t e x :

_ _ s l o t s _ _ = ’ _ e l e m e n t ’

def _ _ i n i t _ _ ( self , x ) : se l f . _ e l e m e n t = x

def e l e m e n t ( s e l f ) : r e t u r n s e l f . _ e l e m e n t

def _ _ h a s h _ _ ( s e l f ) : r e t u r n h a s h(id( s e l f ) )

def _ _ s t r _ _ ( s e l f ) :

r e t u r n str( s e l f . _ e l e m e n t )

30

(36)

Python Implementation

c l a s s Ed g e :

_ _ s l o t s _ _ = ’ _ o r i g i n ’ , ’ _ d e s t i n a t i o n ’ , ’ _ e l e m e n t ’

def _ _ i n i t _ _ ( self , u , v , x ) : se l f . _ o r i g i n = u

se l f . _ d e s t i n a t i o n = v se l f . _ e l e m e n t = x

def e n d p o i n t s ( s e l f ) :

r e t u r n ( s e l f . _origin , s e l f . _ d e s t i n a t i o n )

(37)

Python Implementation

def o p p o s i t e ( self , v ) :

if not i s i n s t a n c e( v , G r a p h . V e r t e x ) : r a i s e T y p e E r r o r ( ’ v m u s t be a V e r t e x ’ ) r e t u r n s e l f . _ d e s t i n a t i o n if v is se l f . _ o r i g i n el s e s e l f . _ o r i g i n

r a i s e V a l u e E r r o r ( ’ v not i n c i d e n t to e d g e ’ )

32

(38)

Python Implementation

def e l e m e n t ( s e l f ) : r e t u r n s e l f . _ e l e m e n t

def _ _ h a s h _ _ ( s e l f ) :

r e t u r n h a s h( ( s e l f . _origin , s e l f . _ d e s t i n a t i o n ) )

def _ _ s t r _ _ ( s e l f ) :

r e t u r n ’ ( { 0 } , { 1 } , { 2 } ) ’ .f o r m a t( s e l f . _origin , s e l f . _ d e s t i n a t i o n , s e l f . _ e l e m e n t )

(39)

Python Implementation

def _ _ i n i t _ _ ( self , d i r e c t e d = F a l s e ) : s e l f . _ o u t g o i n g = {}

s e l f . _ i n c o m i n g = {} if d i r e c t e d e l s e s e l f . _ o u t g o i n g

def _ v a l i d a t e _ v e r t e x ( self , v ) :

if not i s i n s t a n c e( v , s e l f . V e r t e x ) : r a i s e T y p e E r r o r ( ’ V e r t e x e x p e c t e d ’ ) if v not in se l f . _ o u t g o i n g :

r a i s e V a l u e E r r o r ( ’ V e r t e x d o e s not b e l o n g to t h i s g r a p h . ’ )

34

(40)

Python Implementation

def i s _ d i r e c t e d ( s e l f ) :

r e t u r n s e l f . _ i n c o m i n g is not se l f . _ o u t g o i n g

def v e r t e x _ c o u n t ( s e l f ) : r e t u r n len( s e l f . _ o u t g o i n g )

def v e r t i c e s ( s e l f ) :

r e t u r n s e l f . _ o u t g o i n g . k e y s ()

(41)

Python Implementation

def e d g e _ c o u n t ( s e l f ) :

t o t a l = sum(len( s e l f . _ o u t g o i n g [ v ]) for v in s e l f . _ o u t g o i n g )

r e t u r n t o t a l if se l f . i s _ d i r e c t e d () el s e t o t a l // 2

def e d g e s ( s e l f ) : r e s u l t = set()

for s e c o n d a r y _ m a p in se l f . _ o u t g o i n g . v a l u e s () :

r e s u l t . u p d a t e ( s e c o n d a r y _ m a p . v a l u e s () ) r e t u r n r e s u l t

36

(42)

Python Implementation

def g e t _ e d g e ( self , u , v ) : s e l f . _ v a l i d a t e _ v e r t e x ( u ) s e l f . _ v a l i d a t e _ v e r t e x ( v )

r e t u r n s e l f . _ o u t g o i n g [ u ]. get ( v )

def d e g r e e ( self , v , o u t g o i n g = T r u e ) : s e l f . _ v a l i d a t e _ v e r t e x ( v )

adj = s e l f . _ o u t g o i n g if o u t g o i n g el s e se l f . _ i n c o m i n g

r e t u r n len( adj [ v ])

(43)

Python Implementation

def i n c i d e n t _ e d g e s ( self , v , o u t g o i n g = T r u e ) : s e l f . _ v a l i d a t e _ v e r t e x ( v )

adj = s e l f . _ o u t g o i n g if o u t g o i n g el s e se l f . _ i n c o m i n g

for ed g e in adj [ v ]. v a l u e s () : y i e l d e d g e

def i n s e r t _ v e r t e x ( self , x = N o n e ) : v = s e l f . V e r t e x ( x )

s e l f . _ o u t g o i n g [ v ] = {}

if s e l f . i s _ d i r e c t e d () : se l f . _ i n c o m i n g [ v ] = {}

r e t u r n v

38

(44)

Python Implementation

def i n s e r t _ e d g e ( self , u , v , x = N o n e ) : if s e l f . g e t _ e d g e ( u , v ) is not N o n e :

r a i s e V a l u e E r r o r ( ’ u and v are a l r e a d y a d j a c e n t ’ )

e = s e l f . E d g e ( u , v , x ) s e l f . _ o u t g o i n g [ u ][ v ] = e s e l f . _ i n c o m i n g [ v ][ u ] = e

(45)

Graph Traversals

(46)

Graph Traversals

Definition : Graph Traversals

A traversal is a systematic procedure for exploring a graph by exam- ining all of its vertices and edges.

A traversal is efficient if it visits all the vertices and edges in time proportional to their number, that is, in linear time.

(47)

Graph Traversals

Definition : Graph Traversals

A traversal is a systematic procedure for exploring a graph by exam- ining all of its vertices and edges.

A traversal is efficient if it visits all the vertices and edges in time proportional to their number, that is, in linear time.

40

(48)

Graph Traversals

Graph traversal algorithms are key to answering many fundamental questions about graphs involving the notion ofreachability, that is, in determining how to travel from one vertex to another while following paths of a graph.

(49)

Graph Traversals

Interesting problems that deal with reachability in anundirected graph G include the following:

• Computing a path from vertex uto vertexv, or reporting that no such path exists.

• Given a start vertexs ofG, computing, for every vertex v ofG, a path with the minimum number of edges betweens andv, or reporting that no such path exists.

• Testing whetherG is connected.

• Computing a spanning tree of G, ifG is connected.

• Computing the connected components ofG.

• Computing a cycle in G, or reporting thatG has no cycles.

42

(50)

Graph Traversals

Interesting problems that deal with reachability in a directed graph⃗G include the following:

• Computing a directed path from vertexuto vertex v, or reporting that no such path exists.

• Finding all the vertices of⃗G that are reachable from a given vertexs.

• Determine whether⃗G is acyclic.

• Determine whether⃗G is strongly connected.

(51)

Graph Traversals

Depth-First Search

(52)

Depth-First Search

Depth-first search (DFS)is useful for testing a number of properties of graphs, including whether there is a path from one vertex to another and whether or not a graph is connected.

(53)

Depth-First Search

Procedure of DFS

1. Begin ataspecificstartingvertexsinG,andsetsas“visited”.The vertex s is now our “current” vertex - call our current vertex u.

2. Traverse G byconsidering anedge(u,v)incidenttothecurrent vertexu.

• If(u,v)leadstoavisitedvertexv,ignorethatedge.

• If(u,v)leadstoanunvisitedvertexv,gotov.

• Set v as “visited”, and make it the current vertex, repeated the computation above.

• Eventually, we will get to a “dead end”, that is, a current vertex v such that all the edges incident to v lead to visited vertices.

• To get out of this impasse, we backtracking along the edge that brought to v, going back to a previously visited vertex u.

• Wethenmakeu ourcurrentvertexandrepeatthecomputation aboveforanyedgesincidenttouthatwehavenotyetconsidered.

45

(54)

Depth-First Search

• If all ofu’s incident edges lead to visited vertices, then we backtrack to the vertex we came from to get tou, and repeat the procedure at that vertex.

• Thus, we continue to backtrack along the path that we have traced so far until we find a vertex that has yet unexplored edges, take one such edge, and continue the traversal.

• The process terminates when our backtracking leads us back to the start vertexu, and there are no more unexplored edges incident to s.

(55)

Depth-First Search

47

(56)

Depth-First Search

1: Example of depth-first search traversal on an undirected graph starting at vertex A. Assume that a vertex’s adjacencies are considered in alphabetical order.

(57)

Depth-First Search

2:Exampleofdepth-firstsearchtraversalonanundirectedgraphstartingat vertexA.Assumethatavertex’sadjacenciesareconsideredinalphabetical order.

49

(58)

Depth-First Search

3: Example of depth-first search traversal on an undirected graph starting at vertex A. Assume that a vertex’s adjacencies are considered in alphabetical

(59)

Properties of DFS

Proposition

LetG beanundirectedgraphonwhichaDFStraversalstartingata vertexs hasbeenperformed. Then thetraversalvisitsall verticesin the connected component of s, and the discovery edges form a spanningtreeofconnectedcomponentofs.

51

(60)

Properties of DFS

Proposition

Let ⃗G be an directed graph. DFS on⃗G starting at a vertex s visits all the vertices of G that are reachable from s. Also, the DFS tree contains directed paths froms to every vertex reachable froms.

(61)

Graph Traversals

DFS Implementation and Extensions

(62)

DFS Implementation and Extensions

def DFS ( g , u , d i s c o v e r e d ) : for e in g . i n c i d e n t _ e d g e s ( u ) :

v = e . o p p o s i t e ( u )

if v not in d i s c o v e r e d : d i s c o v e r e d [ v ] = e DFS ( g , v , d i s c o v e r e d )

• In order to track which vertices have been visited, and to build a representation of the resulting DFS tree, the implementation introduces a third parameter, named discovered.

• This parameter should be a Python dictionary that maps a vertex to the tree edge that was used to discover that vertex.

(63)

DFS Implementation and Extensions

def DFS ( g , u , d i s c o v e r e d ) : for e in g . i n c i d e n t _ e d g e s ( u ) :

v = e . o p p o s i t e ( u )

if v not in d i s c o v e r e d : d i s c o v e r e d [ v ] = e DFS ( g , v , d i s c o v e r e d )

• In order to track which vertices have been visited, and to build a representation of the resulting DFS tree, the implementation introduces a third parameter, named discovered.

• This parameter should be a Python dictionary that maps a vertex to the tree edge that was used to discover that vertex.

53

(64)

DFS Implementation and Extensions

As a technicality, assume that the source vertexuoccurs as a key of the dictionary, withNone as its value.

A caller might start the traversal as follows: r e s u l t = { u : N o n e }

DFS ( g , u , r e s u l t )

(65)

DFS Implementation and Extensions

As a technicality, assume that the source vertexuoccurs as a key of the dictionary, withNone as its value.

A caller might start the traversal as follows:

r e s u l t = { u : N o n e } DFS ( g , u , r e s u l t )

54

(66)

DFS Implementation and Extensions

The dictionaryresultserves two purposes.

• Internally, the dictionary provides a mechanism for recognizing visited vertices, as they will appear as keys in the dictionary.

• Externally, the DFS function arguments this dictionary as it

proceeds, and thus the values within the dictionary are the DFS tree edges at the conclusion of the process.

(67)

Reconstructing a Path from u to v

We can use the basic DFS function as a tool to identify the (directed) path leading from vertexutov, ifv is reachable from u. This path can easily bereconstructedfrom the information that was recorded in the discoverydictionaryduring the traversal.

56

(68)

Reconstructing a Path from u to v

To reconstruct the path, excecute the following steps:

1. Begin at the end of the path, examining the discovery dictionary to determine what edge was used to reach a vertexv, and then what the other endpoint of that edge is.

2. Add that vertex to a list, and the repeat the process to determine what edge was used to discover it.

3. Once we have traced the path all the way back to the starting vertex u, we can reverse the list so that its is properly oriented fromutov, and return it to the caller.

(69)

Reconstructing a Path from u to v

def c o n s t r u c t _ p a t h ( u , v , d i s c o v e r e d ) : p a t h = []

if v in d i s c o v e r e d : p a t h . a p p e n d ( v ) w a l k = v

w h i l e wa l k is not u : e = d i s c o v e r e d [ w a l k ] p a r e n t = e . o p p o s i t e ( w a l k ) pa t h . a p p e n d ( p a r e n t )

wa l k = p a r e n t p a t h . r e v e r s e () r e t u r n p a t h

58

(70)

Computing all Connected Components

When a graph is not connected, the next goal we may have is to identify all ofconnected components of an undirected graph, or thestrongly connected components of a directed graph.

If an initial call to DFS fails to reach all vertices of a graph, we can restart a new call to DFS at one of those unvisited vertices.

(71)

Computing all Connected Components

When a graph is not connected, the next goal we may have is to identify all ofconnected components of an undirected graph, or thestrongly connected components of a directed graph.

If an initial call to DFS fails to reach all vertices of a graph, we can restart a new call to DFS at one of those unvisited vertices.

59

(72)

Computing all Connected Components

def D F S _ c o m p l e t e ( g ) : f o r e s t = {}

for u in g . v e r t i c e s () : if u not in f o r e s t :

f o r e s t [ u ] = N o n e DFS ( g , u , f o r e s t ) r e t u r n f o r e s t

(73)

Graph Traversals

Breadth-First Search

(74)

Breadth-First Search

The advancing and backtracking of a depth-first search defines a traversal that could bephysically traced by a single person exploring a graph.

Now, we consider another algorithm for traversing a connected component of a graph, known as abreadth-first search (BFS).

(75)

Breadth-First Search

The advancing and backtracking of a depth-first search defines a traversal that could bephysically traced by a single person exploring a graph.

Now, we consider another algorithm for traversing a connected component of a graph, known as abreadth-first search (BFS).

61

(76)

Breadth-First Search

A BFS proceeds in rounds and subdivides the vertices into levels. BFS startsatvertexs,whichisatlevel0.

• In the first round, we set all vertices adjacent to the start vertex s as

“visited”.Theseverticesareonestep awayfromthebeginningand areplaced intolevel1.

• In the second round, we allow all explorers to go two steps away fromthestartingvertex. Thesenewvertices,whichareadjacentto level1 verticesandnotpreviouslyassignedtoalevel,areplacedinto level 2 and marked as “visited”.

• This process continues in similar fashion, terminating when no new verticesarefoundina level.

(77)

Breadth-First Search

4: Example of breadth-first search traversal, where the edges incident to a vertex are considered in alphabetical order of the adjacent vertices.

63

(78)

Breadth-First Search

5: Example of breadth-first search traversal, where the edges incident to a vertex are considered in alphabetical order of the adjacent vertices.

(79)

Breadth-First Search

6: Example of breadth-first search traversal, where the edges incident to a vertexareconsideredinalphabeticalorderoftheadjacentvertices.

65

(80)

Breadth-First Search

7: Example of breadth-first search traversal, where the edges incident to a vertexareconsideredinalphabeticalorderoftheadjacentvertices.

(81)

Breadth-First Search

def BFS ( g , s , d i s c o v e r e d ) : l e v e l = [ s ]

w h i l e len( l e v e l ) > 0:

n e x t _ l e v e l = []

for u in l e v e l :

for e in g . i n c i d e n t _ e d g e s ( u ) : v = e . o p p o s i t e ( u )

if v not in d i s c o v e r e d : d i s c o v e r e d [ v ] = e n e x t _ l e v e l . a p p e n d ( v ) l e v e l = n e x t _ l e v e l

67

(82)

Breadth-First Search

def B F S _ c o m p l e t e ( g ) : f o r e s t = {}

for u in g . v e r t i c e s () : if u not in f o r e s t :

f o r e s t [ u ] = N o n e BFS ( g , u , f o r e s t ) r e t u r n f o r e s t

Referensi

Dokumen terkait

17 Works Approval: W6350/2020/1 IR-T04 Decision Report Template v2.0 July 2017 Risk Events Continue to detailed risk assessment Reasoning Sources/Activities Potential