Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 367 – Introduction to Data Structures

Similar presentations


Presentation on theme: "CS 367 – Introduction to Data Structures"— Presentation transcript:

1 CS 367 – Introduction to Data Structures
Graphs – Part I CS 367 – Introduction to Data Structures

2 Graph Definitions Vertices Edges General graph definition
nodes in the graph Edges connection from one node to another General graph definition a graph is a collection of vertices and edges with little or no restrictions on which vertices are connected together

3 Graph vs. Trees In theory, a tree is a restricted graph
it is laid out in a hierarchical manner each node can have only one parent each node has a set of children from 0 to n siblings have no direct connection In a graph, a “child” can connect directly to its “sibling” and even to its “parent” the quotes are provided because, unlike trees, there is no notion of parent and child

4 Simple and Directed Graphs
A simple graph has a set of vertices connected by a set of edges no notion of direction from one vertex to another an edge from vm to vn is considered the same as and edge from vn to vm A directed graph (or digraph) also has a set of vertices connected by a set of edges now there is a notion of direction an edge from vm to vn is NOT the same as and edge from vn to vm

5 Simple and Directed Graphs
B C B C G F G D D F Simple Graph (B->D) == (D->B) Directed Graph (B->D) != (D->B) (in fact, there is no edge D->B)

6 Multigraph and Psuedograph
A multigraph allows more than one edge between two vertices an edge must have two different vertices on each end A psuedograph is exactly like a multigraph except it allows an edge to have the same vertex at both ends this is called a cycle

7 Multigraph and Psuedograph
B C B C D D Multigraph Psuedograph

8 Weighted Graph A graph where values are associated with each edge
usually considered the “cost” of moving from one vertex to another vertex any type of graph can be converted into a weighted graph

9 Weighted Graph A 8 5 E B 9 C 7 5 4 2 D F

10 Representing a Graph Several ways to represent a graph
need to store vertices need a way to represent edges Two possibilities adjacency list adjacency matrix

11 Adjacency List Stores a list of all vertices adjacent to each vertex
can do this in a table or a linked list Using a table simple solution is to use a 2-D array could use an array (or vector) where each element contains vertex and list Using a linked list each node contains two pointers one pointer to the next vertex one pointer to a list of adjacent vertices

12 Adjacency List – Table A B C D A B A D F E C A D B C D A B C F D E F F

13 Adjacency List – Linked List
B C D B A D F A E C A D B C D A B C F D E F F B E

14 Adjacency Matrix Implemented using a two dimensional square array
each vertex is listed as a row each vertex is listed as a column a 1 in any any element (i, j) indicates the two nodes are adjacent to each other

15 Adjacency Matrix A B C D E F A 1 1 1 A E B 1 1 1 B C C 1 1 D 1 1 1 F D
1 1 1 A E B 1 1 1 B C C 1 1 D 1 1 1 F D E 1 F 1 1

16 Which Representation? Which method to use depends on what is being done processing vertices adjacent to a vertex? quicker to use an adjacency list removing edges? quicker to use an adjacency matrix


Download ppt "CS 367 – Introduction to Data Structures"

Similar presentations


Ads by Google