graphs upon completion you will be able to:

26
Data Structures: A Pseudocode Approach with C, Second Edition 1 Upon completion you will be able to: Understand and use basic graph terminology and concepts Define and discuss basic graph and network structures Design and implement graph and network applications Design and implement applications using the Graphs Graphs

Upload: lorraine-dawson

Post on 17-Jan-2018

227 views

Category:

Documents


0 download

DESCRIPTION

11-1 Basic Concepts Directed and Undirected Graphs Cycles and Loops A graph is a collection of nodes, called vertices, and a collection of segments, called lines or edges, connecting pairs of vertices. In Basic Concepts we develop the terminology and structure for graphs. In addition to the graph definitions, discussion points include: Directed and Undirected Graphs Cycles and Loops Connected and Disjoint Graphs Data Structures: A Pseudocode Approach with C, Second Edition

TRANSCRIPT

Page 1: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 1

Upon completion you will be able to:

• Understand and use basic graph terminology and concepts• Define and discuss basic graph and network structures• Design and implement graph and network applications• Design and implement applications using the graph ADT• Define and discuss Dijkstra's shortest path algorithm

GraphsGraphs

Page 2: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 2

11-1 Basic Concepts

A graph is a collection of nodes, called A graph is a collection of nodes, called verticesvertices, and a collection , and a collection of segments, called of segments, called lines or edgeslines or edges, connecting pairs of vertices. , connecting pairs of vertices. In Basic Concepts we develop the terminology and structure for In Basic Concepts we develop the terminology and structure for graphs. In addition to the graph definitions, discussion points graphs. In addition to the graph definitions, discussion points include:include:

• Directed and Undirected Graphs• Cycles and Loops• Connected and Disjoint Graphs

Page 3: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 3

• A directed graph or digraph for short, is a graph in which line has a direction (arrow head) to its successor. The lines in a directed graphs are known as arcs.

•An undirected graph is a graph in which there is no direction on the lines, known as edges.

Page 4: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 4

•Two vertices in a graph are said to be adjacent vertices if there exists an edge that directly connects them.

•A path is a sequence of vertices in which each vertex is adjacent to the next one. It does not make any difference whether or not the graph is directed, it may still have paths. In an undirected graph, you may travel in either directions. Simple path is a path such that all its vertices and edges are distinct.

•A cycle is a path consisting of at least three vertices that starts and ends with the same vertex.

•A loop is a special case of a cycle in which a single arc begins and ends with the same vertex. In a loop, the end points of the edge are the same.

Page 5: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 5

•Two vertices are said to be connected if there is a path between them. A graph is said to be connected if, suppressing direction, there is a path from any vertex to any other vertex.

•A directed graph is strongly connected if there is a path from each vertex to every other vertex in the digraph.

•A directed graph is weakly connected when there are at least two vertices that are not connected.

• A graph is disjoint if it is not connected.

•The degree of a vertex is the number of lines incident to it.

• The outdegree of a vertex in a digraph is the number of arcs leaving the vertex.

• The indegree is the number of arcs entering the vertex.

Page 6: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 6

11-2 Operations

We define and discuss the six primitive graph operations We define and discuss the six primitive graph operations required to maintain a graph.required to maintain a graph.

• Insert Vertex• Delete Vertex• Add Edge• Delete Edge• Find Vertex• Traverse Graph

Page 7: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 7

Page 8: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 8

Page 9: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 9

Page 10: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 10

Page 11: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 11

Page 12: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 12

Page 13: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 13

1. We begin by pushing the 1st vertex, A, into the stack.2. We then loop, popping the stack and after processing the

vertex, pushing all of the adjacent vertices into the stack. To process X at step 2, we pop X from the stack, process it and then push G and H into the stack giving the stack contents for step 3 to process H and G.

3. When the stack is empty, the traversal is complete.

Page 14: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 14

Page 15: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 15

1. We begin by enqueuing vertex, A, in the queue.2. We then loop, dequeuing the queue and processing the

vertex from the front of the queue. After processing the vertex, we place all of its adjacent vertices into the queue. We are then ready for the next step.

3. When the queue is empty, the traversal is complete.

Page 16: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 16

11-3 Graph Storage Structures

To represent a graph, we need to store two sets. The first set To represent a graph, we need to store two sets. The first set represents the vertices of the graph, and the second set represents the vertices of the graph, and the second set represents the edges or arcs. The two most common structures represents the edges or arcs. The two most common structures used to store these sets are arrays and linked lists.used to store these sets are arrays and linked lists.

• Adjacency Matrix• Adjacency List

Page 17: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 17

Page 18: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 18

Page 19: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 19

11-6 Networks

A network is a graph whose lines are weighted. It is also known A network is a graph whose lines are weighted. It is also known as a weighted graph. Included in this section are two graph as a weighted graph. Included in this section are two graph applications that process networks.applications that process networks.

• Minimum Spanning Tree• Shortest Path Algorithm

Page 20: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 20

Page 21: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 21

Page 22: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 22

Page 23: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 23

Minimum Spanning Tree A spanning tree is a tree that contains

all of the vertices in a graph. A minimum spanning tree is a

spanning tree in which the total weight of the lines are guaranteed to be the minimum of all possible trees in the graph. If the weights are unique, then there will be only one minimum spanning tree.

Page 24: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 24

Algorithm of MST (Prim Algoritm)1. Insert the first vertex (pick any vertex) into the tree2. From every vertices already in the tree, examine the edges that connected to all adjacent vertices not in the tree. Select the edge with the minimum weight to a vertex not currently in the tree. Insert that minimum-weight edge and the vertex into the tree. 3. Repeat step 2 until all vertices are in the tree.

Page 25: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 25

Page 26: Graphs Upon completion you will be able to:

Data Structures: A Pseudocode Approach with C, Second Edition 26

(continued)