220 likes | 241 Views
Terminology and Definitions. Given graph G = (V, E), collections of edges are called: . Walks (open: start ≠ end) if repeated vertices, repeated edges. . Walks (closed: start = end) if repeated vertices, repeated edges. . Trails if repeated vertices, no repeated edges, open.
E N D
Terminology and Definitions • Given graph G = (V, E), collections of edges are called: • . Walks (open: start ≠ end) if repeated vertices, repeated edges. • . Walks (closed: start = end) if repeated vertices, repeated edges. • . Trails if repeated vertices, no repeated edges, open. • . Circuits if repeated vertices, no repeated edges, closed. • . Paths if no repeated vertices, no repeated edges, open. • . Cycles if no repeated vertices, no repeated edges, closed.
Example • Open walk: (3, 4), (4, 5), (5, 3), (3, 4) • Closed walk: (4, 1), (1, 3), (3, 4), (4, 5), (5, 3), (3, 4) • Trail: (1, 2), (2, 4), (4, 1), (1, 3) • Circuit: (4, 1), (1, 2), (2, 4), (4, 5), (5, 3), (3, 4) • Path: (5, 3), (3, 4), (4, 1), (1, 2) • Cycle: (5, 3), (3, 4), (4, 5) 2 1 3 4 5 All cycles are circuits. All circuits are closed walks. All paths are trails. All trails are open walks.
Definitions • A graph G is connected if there exists an undirected path between any two distinct vertices. • A graph G is acyclic if it contains no cycles. • The degree of vertex, v, is deg(v), the number of edges incident to that vertex. Indegree for edges coming in. • Outdegree for edges going out. 3 2 6 1 4 5
Examples 2 1 • G1= (N1, E1) • N1 = {1, 2, 3, 4} • E1 = {(1,2), (1,4), (2,4), (3,1), (3, 4)} 3 4 a • G2= (N2, E2) • N2 = {a, b, c} • E2 = {{a,b}, {a,c}, {b,c}} b c
Trees • Loop-free: no edge exists from vertex v back to itself • Connected and acyclic for undirected graph G • A subgraph of G is called a spanning tree is the subgraph is a tree containing all the vertices of G but the original graph is not. • Vertices with degree 1 are called leaves.
Examples? b b a a b a c c c e d e d e d f f f
Tree Properties • If a and b are distinct vertices, then there is a unique path that connects these vertices. • If G is undirected, then G is connected if and only if G has a spanning tree. • Given a tree. T = (V, E), |V| = |E| + 1 • A tree has at least two vertices with degree 1.
Directed Trees • Directed graph G is a directed tree if corresponding undirected graph is a tree. • Rooted tree if there is one vertex designated the root. • Directed-out tree if root has indegree of 0 and every other vertex has indegree of 1. 1 Directed-out tree at vertex 1 3 2 1 4 Rooted tree at vertex 1 Rooted tree at vertex 4 4 1 3 3 2 2 1 4
Representing Networks Given a directed graph, or network, some ways to represent mathematically: 1. Vertex-edge incidence matrix 2. Adjacency matrix 3. Adjaceny list c 4 2 a d f 1 b 3 5 e
Vertex-edge incidence matrix a b c d e f 1 1 2 -1 3 0 4 0 5 0 c 4 2 a d f 1 b 3 5 e
Adjacency matrix to 1 2 3 4 5 from 1 0 2 0 3 0 4 0 5 0 c 4 2 a d f 1 b 3 5 e
Adjacency List 1: 2: 3: 4: 5: c 4 2 a d f 1 b 3 5 e
Operations/arrays/info needed for Network Algorithms cij i j uij • In order to access information from a network concerning vertices and edges, • with e as the current edge from i to j being examined. • x := Tail(e) • y := Head(e) • cost := Cost(e) • capacity := Cap(e) • connect(i) = true (or false) if there is (not) a path from some given vertex to i. • pred(i) = the vertex preceding i along a given path from some given vertex. • n = number of vertices in G • m = number of edges in G
Graph Search Questions • Is there a path from vertex i to vertex j? • Is graph G connected? • Is there a directed path from vertex i to vertex j? • Is directed graph G strongly connected? • If not connected, what are the components of G? • Graph Search Algorithms • Given: G, described by adjacency list, E(j), for all vertices j, and specified node, s. • Output: Component of G containing s.
Basic Graph Search/Path Algorithm begin mark each node as unconnected mark s and set its predecessor to 0 create LIST of marked vertices and add s to LIST while LIST is not empty do begin select vertex j from LIST select edges (j, k) from adjacency list, E(j) mark each unconnected vertex k, then set its predecessor to j add each vertex k to LIST after adjacency list, E(j), is examined, remove vertex j from LIST end end If looking for an s-t path, include statement that if k=t, then path found
Two types of LIST • As a Queue: first in first out (FIFO) results in a breadth-first search. • Vertices get added to the back of the list but get selected from the front. • As a Stack: last in first out (LIFO) results in a depth-first search. • Vertices get added to the front of the list and selected from the from.
Example with Breadth-first search, s = 1 LIST:{1} 1 6 LIST:{1, 2, 4, 6} 4 LIST:{2, 4, 6, 3, 5} 2 7 LIST:{4, 6, 3, 5, 7} 1 5 LIST:{6, 3, 5, 7} 3 8 2 4 6 LIST:{3, 5, 7, 8} LIST:{5, 7, 8} 3 5 7 LIST:{7, 8} 8 LIST:{8}
Example with Depth-first search, s = 1 1 LIST:{1} 1 6 LIST:{2, 1} 2 4 LIST:{3, 2, 1} 3 2 7 LIST:{5, 3, 2, 1} 5 LIST:{7, 5, 3, 2, 1} 5 3 8 LIST:{4, 7, 5, 3, 2, 1} 7 LIST:{6, 4, 7, 5, 3, 2, 1} 4 8 LIST:{8, 7, 5, 3, 2, 1 } 6
1 2 4 5 3 Example with directed network, s = 2 4 2 Given a directed graph, and source s that cannot reach every vertex, a cutset is formed. Set S contains s and all reachable vertices. Set V - S contains those vertices not reachable. 1 3 5 V-S S
Topolgical Order • Is there a special way to number the vertices of graph? • If G is acyclic, then there is a source vertex (vertex with indegree of 0) • If G is acyclic, then vertices can be labeled so that each edge (j, k) has j < k. • Such an ordering of vertices is called a topological order of G. • G is acyclic if and only if G has a topological order. • Key to finding topological order is to successively remove sources. • Topological Order Algorithm • Given: G, described by adjacency list, E(j), for all vertices j. • Output: topological order of G, if it exists.
Topological Order Algorithm begin mark each vertex with indegree of 0 run through adjacency list and increase indegree of Head(e) by 1 create LIST of vertices with indegree of 0 set position = 0 while LIST is not empty do begin select vertex j from LIST increase position by 1 set the order of j to position value select edges (j, k) from adjacency list, E(j) decrease the value of indegree for vertex k by 1 if new indegree value is 0, add vertex k to LIST after adjacency list, E(j), is examined, remove vertex j from LIST end if position < n, then G is not acyclic, else return topological order end