90 likes | 149 Views
12.7 – Graphs. pg 244 - 249. by Sarah Spencer. Terminology. Graph – a collection of vertices and a collection of line segments. Digraph – directed graph – each line has an arrowhead to its successor. Undirected graph – a graph whose lines have no direction
E N D
12.7 – Graphs pg 244 - 249 by Sarah Spencer
Terminology • Graph – a collection of vertices and a collection of line segments. • Digraph – directed graph – each line has an arrowhead to its successor. • Undirected graph – a graph whose lines have no direction • Arcs – the line segments that are directional • Edges – the line segments that have no direction • Path – a sequence of vertices in which each vertex is adjacent to the next one • Connected graph – if, regardless of direction, there is a path from any vertex to any other vertex
Add/Delete Vertex A B A B C C D D- to be added • The add vertex operation allows you add a vertex to the graph. However, no connecting edges are added in this operation. • The delete vertex operation allows you to delete a vertex from a graph. With this operation, all connecting lines are also deleted.
Add/Delete Edge A B A B C D C D • The Add edge operation allows you to add an edge to either connection a vertices to the rest of the graph or make a new connection between two vertices. • The delete edge operation allows you to delete an edge in order to break the connection between two vertices
Find Vertex A B A B C D C D - The find vertex operation transverses a graph looking for a specified vertex. Once that vertex is found, the computer read the data from only that vertex.
Depth-First Traveral 1 A 6 3 D 2 Z B 5 X 8 9 C Y 4 W E 7 In the depth-first traversal, you pick the first vertex and process it. Next, you select any vertex adjacent to the first vertex and process it. Then, you pick any vertex adjacent to the second vertex. You keep doing this until you get to a vertex that has no adjacent vertices. When this happens, you go back to the previous vertex and pick a different adjacent vertex that hasn’t already been processed. Keep following this pattern until all the vertices are processed.
Breath-First Traversal 1 A 8 4 D 2 Z B 6 X 9 3 C Y 5 W E 7 In the breath-first traversal, you pick the first vertex and process it. Next, you process all the adjacent vertices. Then, you pick one vertex that is adjacent to the first vertex and process all the vertices adjacent to that vertex. You continue this pattern until all the vertices are processed.
Implementation of a Graph • To represent a graph, you need two sets of data, one to represent the vertices and another to represent the edges or arcs. • The two most common structures used to store these sets are the adjacency matrix and adjacency list
Graph Applications • Networks – a graph with weighted lines. Weighted lines are numbers that, for example, show the distance between networks in an internetwork. Networks are often used by airlines in their organization of flights. • Spanning tree – a tree that contains all the vertices on the graph. Most of the time, a minimum spanning tree is used. A minimum spanning tree is a spanning tree such that the sum of the weights is the minimum. For example, this application may be used to figure out to minimum amount of wire that is needed to connect all the computers in a network.