(20) Graphs

# (20) Graphs - Reading CMSC 132 Object-Oriented Programming...

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

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 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 A B

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

View Full Document
5 More graph definitions A directed graph has directed edges An undirected graph has undirected edges A B A B 6 More graph definitions A weighted graph has a weight or cost associated with each edge 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 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
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 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) 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 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

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

View Full Document
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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern