CS 225 Data Structures No Novem ember er 12 – Gr Graphs aphs Wa Wade Fagen-Ul Ulmschneider
Gr Grap aphs To study all of these structures: 1. A common vocabulary 2. Graph implementations 3. Graph traversals 4. Graph algorithms
Incident Edges: Grap Gr aph V Vocab abular lary I(v) = { (x, v) in E } Degree(v): |I| G = (V, E) |V| = n Adjacent Vertices: |E| = m (2, 5) A(v) = { x : (x, v) in E } Path(G 2 ): Sequence of vertices connected by edges Cycle(G 1 ): Path with a common begin and end vertex. G 3 Simple Graph(G): A graph with G 2 no self loops or multi-edges. G 1
Subgraph(G): Gr Grap aph V Vocab abular lary G’ = (V’, E’): V’ ∈ V, E’ ∈ E, and G = (V, E) (u, v) ∈ E à u ∈ V’, v ∈ V’ |V| = n |E| = m (2, 5) Complete subgraph(G) Connected subgraph(G) Connected component(G) Acyclic subgraph(G) Spanning tree(G) G 3 G 2 G 1
Running times are often reported by n , the number of vertices, but often depend on m , the number of edges. How many edges? Minimum edges: Not Connected: V Connected*: a b U d X Z h c e Maximum edges: W g Simple: f Y Not simple:
Con Connected Graphs
Pr Proving the size of a minimally connected graph Theorem: Every connected graph G=(V, E) has at least |V|-1 edges.
Thm: Every connected graph G=(V, E) has at least |V|-1 edges. Proof: Consider an arbitrary, connected graph G=(V, E) .
Suppose |V| = 1: Definition: A connected graph of 1 vertex has 0 edges. Theorem: |V|-1 edges è 1-1 = 0.
Inductive Hypothesis: For any j < |V| , any connected graph of j vertices has at least j-1 edges.
Suppose |V| > 1: 1. Choose any vertex: V a 2. Partition: U X Z h c e W f Y
Suppose |V| > 1: V a 3. Count the edges U X Z h c e W f Y
Gr Grap aph A ADT Functions: - insertVertex(K key); Data: - insertEdge(Vertex v1, Vertex v2, K key); - Vertices - Edges - removeVertex(Vertex v); - Some data structure - removeEdge(Vertex v1, Vertex v2); maintaining the structure between vertices and edges. - incidentEdges(Vertex v); - areAdjacent(Vertex v1, Vertex v2); V b d X Z h - origin(Edge e); e - destination(Edge e); W g f Y
Gr Grap aph I Imple lementatio tion: E : Edge L Lis ist u a c b d insertVertex(K key); v w z removeVertex(Vertex v); u a areAdjacent(Vertex v1, Vertex v2); v b w c incidentEdges(Vertex v); z d
Gr Grap aph I Imple lementatio tion: A : Adjace acency cy M Matr trix ix insertVertex(K key); u removeVertex(Vertex v); a c areAdjacent(Vertex v1, Vertex v2); b d incidentEdges(Vertex v); v w z u v w z u a u v b v w c w z d z
Recommend
More recommend