# ========================================== #
# Pour tester l'importation de fichiers CSV. #
# Version de décembre 2017                   #
# ========================================== #

# RAPPEL : les données des fichiers CSV importés correspondent à des matrices
# d'adjacence ou à des listes d'arêtes.

## Variable chemin pour accéder aux fichiers CSV
# IMPORTANT : pour vos propre tests, il convient d'adapter le chemin pour
# accéder aux fichiers CSV !
chemin = 'C:\\Documents and Settings\\Propriétaire\\Mes documents\\MATHEMATIQUES\\ENSEIGNEMENT\\FENELON\\CPGE\\INFORMATIQUE\\TD et thèmes\\Graphes et arbres\\'

## Importations
import csv
from numpy import zeros, int8

## Lecture d'un fichier CSV : 1ère approche (sans utilisation du module csv)
# Fichier CSV correspondant à une matrice d'adjacence
ObjetFichierCSV = open(chemin + 'Graphe_1_matrice.csv','r')

L = []
for e in ObjetFichierCSV:
    L.append(e.split(','))

n = len(L)
A = zeros((n,n),dtype=int8)
for i  in range(n):
    for j in range(n):
        A[i,j] = L[i][j]
print("Première matrice d'adjacence.")
print(A)

# Fichier CSV correspondant à une liste
# Remarque : la première ligne fournit l'ordre du graphe.
ObjetFichierCSV = open(chemin + 'Graphe_2_liste.csv','r')

L = []
for e in ObjetFichierCSV:
    L.append(e.split(','))

n = int(L.pop(0)[0])
A = zeros((n,n),dtype=int8)
for e  in L:
    A[e[0],e[1]], A[e[1],e[0]] = 1, 1

print("Deuxième matrice d'adjacence.")
print(A)

## Lecture d'un fichier CSV : 2ème approche (avec utilisation du module csv)
ObjetFichierCSV = csv.reader(open(chemin + 'Dijkstra_1.csv',newline=''),quoting=csv.QUOTE_NONNUMERIC)

L = []
for row in ObjetFichierCSV:
    L += [row]

n = len(L)
A = zeros((n,n),dtype=int8)
for i in range(n):
    for j in range(n):
        A[i,j] = L[i][j]

print("Matrice des poids (pour l'algorithme de Dijkstra).")
print(A)