(20) Graphs

(20) Graphs - 1 CMSC 132: Object-Oriented Programming II...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CMSC 132: Object-Oriented Programming II Graphs & Graph Traversals Department of Computer Science University of Maryland, College Park Reading Read Chapter 12 in the Koffman text (Chapter 10 in the second edition) 2 3 Graph data structures- review A many-to-many relationship between elements Each element may have multiple predecessors Each element may have multiple successors many successors many predecessors 3 4 Graph definitions A node or vertex is an element of a graph Its state is a list of the adjacent (neighbor or successor) nodes An edge is a connection between two nodes Its state is the nodes that are its endpoints A 4 A B 5 More graph definitions A directed graph has directed edges An undirected graph has undirected edges 5 A B A B 6 More graph definitions A weighted graph has a weight or cost associated with each edge 6 7 More graph definitions A path is a sequence of nodes n 1 , n 2 , … n k in a graph such that an edge exists between each pair of nodes n i , n i+1 in the path Example: A, B, C is a path A, E, D is not a path 7 8 More graph definitions A cycle is a path that ends back at the starting node Examples: A, E, A A, B, C, D, E, A A simple path is a path that has no cycles An acyclic graph is a graph that has no cycles 8 9 More graph definitions In a connected graph every node in the graph is reachable from every other node in the graph An unconnected graph has several disjoint components 9 10 Some graph operations Adding or removing nodes or edges Traversal (search) of a graph refers to visiting each node in it, usually visiting each node just once Some type of computation is typically performed at each node Two approaches: Breadth-first search (BFS) Depth-first search (DFS) 10 11 Breadth-first search (BFS) Approach- visit all neighbors of a node first View as a series of expanding circles Keep the list of nodes to visit in a queue Example traversal: 1. N 2. A, C, B 3. E, G, H, I, J 4. D, F 11 12 Breadth-first tree traversal Example traversals starting from 1 1 2 3 4 5 6 7 1 3 2 6 5 4 7 1 2 3 5 6 4 7 left to right right to left random 12 13 Traversal orders Order of neighbors For a tree, the children can be ordered from left to right For a graph left to right doesn’t make much sense Each node just has a set of neighbors and predecessors; there is no order among edges For breadth-first search Visit all nodes at distance k from starting point...
View Full Document

This note was uploaded on 11/16/2011 for the course CMSC 132 taught by Professor Padua-perez during the Fall '08 term at Maryland.

Page1 / 11

(20) Graphs - 1 CMSC 132: Object-Oriented Programming II...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online