Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Definition: Directed graph
4
} Directed Graph} A directed graph, or digraph, is a pair} G = (V, E)} whereV is a set whose elements are called vertices, and} E is a set of ordered pairs of elements ofV.
} Vertices are often also called nodes.} Elements of E are called edges, or directed edges, or arcs.} For directed edge (v, w) in E, v is its tail and w its head;} (v, w) is represented in the diagrams as the arrow, v -> w.} In text we simple write vw.
Definition: Undirected graph
5
} Undirected Graph} A undirected graph is a pair} G = (V, E)} whereV is a set whose elements are called vertices, and} E is a set of unordered pairs of distinct elements ofV.
} Vertices are often also called nodes.} Elements of E are called edges, or undirected edges.} Each edge may be considered as a subset ofV containing two elements,} {v, w} denotes an undirected edge} In diagrams this edge is the line v---w.} In text we simple write vw, or wv} vw is said to be incident upon the vertices v and w
Terms You Should Know
6
} Vertex (plural vertices) or Node} Edge (sometimes referred to as an arc)
} Note the meaning of incident} Degree of a vertex: how many adjacent vertices
} Digraph: in-degree (num. of incoming edges) vs. out-degree} Graphs can be:
} Directed or undirected} Weighted or not weighted
} weights can be reals, integers, etc.} weight also known as: cost, length, distance, capacity,…
} Undirected graphs:} Normally an edge can’t connect a vertex to itself
} A directed graph (also known as a digraph)} “Originating” node is the head, the target the tail} An edge may connect a vertex to itself
Terms You Should Know or Learn Now
7
} Size of graph? Two measures:} Number of nodes. Usually ‘V’} Number of edges: usually ‘E’
} Dense graph: many edges} Maximally dense?} Undirected: each node connects to all others, so
e = v(v-1)/2Called a complete graph
} Directed: e = v(v-1) why?
} Sparse graph: fewer edges} Could be zero edges…
Terms You Should Know or Learn Now
8
} Path vs. simple path} One vertex is reachable from another vertex
} A connected graph} undirected graph, where each vertex is reachable from all others
} A strongly connected digraph:} direction affects this!} node u may be reachable from v, but not v from u} Strongly connected means both directions
} Connected components for undirected graphs
Terms You Should Know or Learn Now
9
} Cycle} Directed graph: non-empty path with same starting and ending node} An edge may appear more than once (but why?)
} Simple cycle: no node repeated except start and end} Undirected graph: same idea
} If an edge appears more than once (I.e. non-simple) then we traverse it in the same direction
} Acyclic: no-cycles} A connected, acyclic undirected graph: free tree
} If we specificy a root, it’s a rooted tree} Acyclic but not connected? a undirected forest
} Directed acyclic graph: a DAG
Self-test: Understand these Terms?
10
} Subgraph} Symmetric digraph} complete graph} Adjacency relation} Path, simple path, reachable} Connected, Strongly Connected} Cycle, simple cycle} acyclic} undirected forest} free tree, undirected tree} rooted tree} Connected component
Definitions: Weighted Graph
11
} A weighted graph is a triple (V, E,W)} where (V, E) is a graph (directed or undirected) and} W is a function
from E into R, the reals (integer or rationals).
} For an edge e, W(e) is called the weight of e.
Graph Representations using Data Structures
12
} Adjacency Matrix Representation} Let G = (V,E), n = |V|, m = |E|,V = {v1, v2,…, vn)} G can be represented by an n ´ n matrix
Traversing Graphs
17
} “Traversing” means processing each vertex edge in some organized fashion byfollowing edges between vertices} We speak of visiting a vertex. Might do something while there.
} Recall traversal of binary trees:} Several strategies: In-order, pre-order, post-order} Traversal strategy implies an order of visits} We used recursion to describe and implement these
} Graphs can be used to model interesting, complex relationships} Often traversal used just to process the set of vertices or edges} Sometimes traversal can identify interesting properties of the graph} Sometimes traversal (perhaps modified, enhanced) can answer interesting questions about theproblem-instance that the graph models
BFS: Overall Strategy
18
} Breadth-first search: Strategy} choose a starting vertex, distance d = 0} vertices are visited in order of increasing distance from the starting vertex,} examine all edges leading from vertices (at distance d) to adjacent vertices (at distance
d+1)} then, examine all edges leading from vertices at distance d+1 to distance d+2, and so
on,} until no new vertex is discovered
BFS: Specific Input/Output
19
} Input:} A graph G} single start vertex s
} Output:} Shortest distance from s to each node in G (distance = number of edges)} Breadth-First Tree of G with root s
} Note:The paths in this BFS tree represent the shortest paths from s to each node in G
Breadth-first search implementation
21
} Vertices here have some properties:} color = white/gray/black} d = distance from start node} pi = node through which d is achieved
Breadth-first search: Analysis
22
} For a digraph havingV vertices and E edges} Each edge is processed once in the while loop for a cost of q(E)} Each vertex is put into the queue once and removed from the queue and processed
once, for a cost q(V)} Total: q(V+E)} Extra space is used for color array and queue, there are q(V)
} From a tree (breadth-first spanning tree)} the path in the tree from start vertex to any vertex contains the minimum possible
number of edges} Not all vertices are necessarily reachable from a selected starting vertex
Breadth-first search: Some Properties
23
} Does BFS always compute ẟ(s,v) correctly, where ẟ(s,v) is the shortest path(number of edges) from s to any vertex v?
} Lemma 1:
Let G=(V,E) be a directed or undirected graph, and let 𝑠 𝜖 V be an arbitraryvertex.Then, for any edge 𝑢, 𝑣 𝜖 𝐸
ẟ(s,v) ≤ ẟ(s,u) + 1
Breadth-first search: Some Properties
24
} Lemma 2:
Let G = (V,E) be a directed or undirected graph, and suppose BFS is run on Gfrom a given source vertex 𝑠 ∈ 𝑉, Then upon termination, for each vertex 𝑣 ∈ 𝑉,the value v.d computed by BFS satisfies 𝑣. 𝑑 ≥ 𝛿(𝑠, 𝑣)
^^^^This is a weak bound! Just says distance will not be better than best path.
//By how code updates v.d//By inductive hypothesis//By Lemma 1 on previous slide
Breadth-first search: Some Properties
25
} Lemma 3:
Suppose during BFS execution, the Queue contains vertices {v1,v2,….vn} wherev1 is at head of queue and vn is at tail of queue.Then:
𝑣!. 𝑑 ≤ 𝑣". 𝑑 + 1 //all nodes on Q differ by at most 1𝑣# . 𝑑 ≤ 𝑣#$". 𝑑 //nodes on Q are non-decreasing distances
for i = 1,2,3,….,n-1
Why?
Proof of Correctness
27
} Claim:
} Let G=(V,E) be a directed or undirected graph, and suppose that BFS is run on Gfrom a given source vertex 𝑠 ∈ 𝑉. Then, during its execution, BFS discovers everyvertex 𝑣 ∈ 𝑉 that is reachable from s, and upon termination 𝑣. 𝑑 = 𝛿(𝑠, 𝑣) forall 𝑣 ∈ 𝑉.
Proof of Correctness
28
} Proof by Contradiction:
} Assume that BFS does NOT work.} Then…there MUST exist at least one node v such that 𝑣. 𝑑 ≠ 𝛿(𝑠, 𝑣)
} There might be more, but let v be such a node with the smallest v.d value} Meaning the ”first one” that BFS incorrectly calculates.} This is a good choice because we can assume all nodes with smaller d value were
computed correctly! Nice!
Proof of Correctness
29
} So, this incorrectly calculated node v has the following property:
𝑣. 𝑑 > 𝛿 𝑠, 𝑣 = 𝛿 𝑠, 𝑢 + 1 = 𝑢. 𝑑 + 1
Because of Lemma 2! By definition ofoptimal path
By how we chosev
Proof of Correctness
30
𝑣. 𝑑 > 𝛿 𝑠, 𝑣 = 𝛿 𝑠, 𝑢 + 1 = 𝑢. 𝑑 + 1
So…at some point during execution. The node u is popped off the queue and theedge e=(u,v) is followed and node v is processed.Three cases:
Case 1: v is whiteCase 2: v is grayCase 3: v is black
Proof of Correctness
31
𝑣. 𝑑 > 𝛿 𝑠, 𝑣 = 𝛿 𝑠, 𝑢 + 1 = 𝑢. 𝑑 + 1
Case 1: v is white
If v is white, algorithm sets 𝑣. 𝑑 = 𝑢. 𝑑 + 1 (line 15).
Contradiction! above formula shows 𝑣. 𝑑 > 𝑢. 𝑑 + 1
Proof of Correctness
32
𝑣. 𝑑 > 𝛿 𝑠, 𝑣 = 𝛿 𝑠, 𝑢 + 1 = 𝑢. 𝑑 + 1
Case 2: v is gray
if v is gray, then v is currently on the queue.v was turned gray by dequeuing some other node w, setting 𝑣. 𝑑 = 𝑤. 𝑑 + 1Order on queue: w, then u, then v, Lemma 3 gives 𝑤. 𝑑 ≤ 𝑢. 𝑑 ≤ 𝑣. 𝑑So: 𝑣. 𝑑 = 𝑤. 𝑑 + 1 ≤ 𝑢. 𝑑 + 1
^^Contradiction!
Proof of Correctness
33
𝑣. 𝑑 > 𝛿 𝑠, 𝑣 = 𝛿 𝑠, 𝑢 + 1 = 𝑢. 𝑑 + 1
Case 3: v is black
if v is black, then v was previously on queue ahead of uqueue distance values monotonically increasing, so 𝑣. 𝑑 ≤ 𝑢. 𝑑 (Lemma 3)Thus 𝑣. 𝑑 ≤ 𝑢. 𝑑 < 𝑢. 𝑑 + 1
^^Contradiction!!